Skip to content

Commit 36191a8

Browse files
committed
Merge #12461: scripted-diff: Rename key size consts to be relative to their class
0580f86 Fixup whitespace (Ben Woosley) 47101bb scripted-diff: Rename CPubKey and CKey::*_KEY_SIZE and COMPRESSED_*_KEY_SIZE (Ben Woosley) Pull request description: ~~And introduce CPubKeySig to host code relative to key sigs.~~ ACKs for top commit: meshcollider: utACK bitcoin/bitcoin@0580f86 Tree-SHA512: 29aa0be54912358b138e391b9db78639786f56580493e590ec9f773c0e1b421740133d05a79be247c7ee57e71c9c9e41b9cb54088cb3c0e3f813f74f0895287b
2 parents 6496bb8 + 0580f86 commit 36191a8

File tree

8 files changed

+45
-45
lines changed

8 files changed

+45
-45
lines changed

src/key.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
8383
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
8484
* included.
8585
*
86-
* privkey must point to an output buffer of length at least CKey::PRIVATE_KEY_SIZE bytes.
86+
* privkey must point to an output buffer of length at least CKey::SIZE bytes.
8787
* privkeylen must initially be set to the size of the privkey buffer. Upon return it
8888
* will be set to the number of bytes used in the buffer.
8989
* key32 must point to a 32-byte raw private key.
9090
*/
9191
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, bool compressed) {
92-
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
92+
assert(*privkeylen >= CKey::SIZE);
9393
secp256k1_pubkey pubkey;
9494
size_t pubkeylen = 0;
9595
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
@@ -115,11 +115,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
115115
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
116116
memcpy(ptr, key32, 32); ptr += 32;
117117
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
118-
pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE;
118+
pubkeylen = CPubKey::COMPRESSED_SIZE;
119119
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
120120
ptr += pubkeylen;
121121
*privkeylen = ptr - privkey;
122-
assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
122+
assert(*privkeylen == CKey::COMPRESSED_SIZE);
123123
} else {
124124
static const unsigned char begin[] = {
125125
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
@@ -141,11 +141,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
141141
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
142142
memcpy(ptr, key32, 32); ptr += 32;
143143
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
144-
pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
144+
pubkeylen = CPubKey::SIZE;
145145
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
146146
ptr += pubkeylen;
147147
*privkeylen = ptr - privkey;
148-
assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
148+
assert(*privkeylen == CKey::SIZE);
149149
}
150150
return 1;
151151
}
@@ -173,8 +173,8 @@ CPrivKey CKey::GetPrivKey() const {
173173
CPrivKey privkey;
174174
int ret;
175175
size_t privkeylen;
176-
privkey.resize(PRIVATE_KEY_SIZE);
177-
privkeylen = PRIVATE_KEY_SIZE;
176+
privkey.resize(SIZE);
177+
privkeylen = SIZE;
178178
ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
179179
assert(ret);
180180
privkey.resize(privkeylen);
@@ -184,7 +184,7 @@ CPrivKey CKey::GetPrivKey() const {
184184
CPubKey CKey::GetPubKey() const {
185185
assert(fValid);
186186
secp256k1_pubkey pubkey;
187-
size_t clen = CPubKey::PUBLIC_KEY_SIZE;
187+
size_t clen = CPubKey::SIZE;
188188
CPubKey result;
189189
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
190190
assert(ret);
@@ -276,7 +276,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
276276
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
277277
if ((nChild >> 31) == 0) {
278278
CPubKey pubkey = GetPubKey();
279-
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
279+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
280280
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
281281
} else {
282282
assert(size() == 32);

src/key.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* secure_allocator is defined in allocators.h
2121
* CPrivKey is a serialized private key, with all parameters included
22-
* (PRIVATE_KEY_SIZE bytes)
22+
* (SIZE bytes)
2323
*/
2424
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
2525

@@ -30,15 +30,15 @@ class CKey
3030
/**
3131
* secp256k1:
3232
*/
33-
static const unsigned int PRIVATE_KEY_SIZE = 279;
34-
static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
33+
static const unsigned int SIZE = 279;
34+
static const unsigned int COMPRESSED_SIZE = 214;
3535
/**
3636
* see www.keylength.com
3737
* script supports up to 75 for single byte push
3838
*/
3939
static_assert(
40-
PRIVATE_KEY_SIZE >= COMPRESSED_PRIVATE_KEY_SIZE,
41-
"COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
40+
SIZE >= COMPRESSED_SIZE,
41+
"COMPRESSED_SIZE is larger than SIZE");
4242

4343
private:
4444
//! Whether this private key is valid. We check for correctness when modifying the key

src/psbt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct PSBTInput
171171
case PSBT_IN_PARTIAL_SIG:
172172
{
173173
// Make sure that the key is the size of pubkey + 1
174-
if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
174+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
175175
throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey");
176176
}
177177
// Read in the pubkey from key

src/pubkey.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
196196
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
197197
return false;
198198
}
199-
unsigned char pub[PUBLIC_KEY_SIZE];
200-
size_t publen = PUBLIC_KEY_SIZE;
199+
unsigned char pub[SIZE];
200+
size_t publen = SIZE;
201201
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
202202
Set(pub, pub + publen);
203203
return true;
@@ -217,8 +217,8 @@ bool CPubKey::Decompress() {
217217
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
218218
return false;
219219
}
220-
unsigned char pub[PUBLIC_KEY_SIZE];
221-
size_t publen = PUBLIC_KEY_SIZE;
220+
unsigned char pub[SIZE];
221+
size_t publen = SIZE;
222222
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
223223
Set(pub, pub + publen);
224224
return true;
@@ -227,7 +227,7 @@ bool CPubKey::Decompress() {
227227
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
228228
assert(IsValid());
229229
assert((nChild >> 31) == 0);
230-
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE);
230+
assert(size() == COMPRESSED_SIZE);
231231
unsigned char out[64];
232232
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
233233
memcpy(ccChild.begin(), out+32, 32);
@@ -238,8 +238,8 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
238238
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
239239
return false;
240240
}
241-
unsigned char pub[COMPRESSED_PUBLIC_KEY_SIZE];
242-
size_t publen = COMPRESSED_PUBLIC_KEY_SIZE;
241+
unsigned char pub[COMPRESSED_SIZE];
242+
size_t publen = COMPRESSED_SIZE;
243243
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
244244
pubkeyChild.Set(pub, pub + publen);
245245
return true;
@@ -251,8 +251,8 @@ void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
251251
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
252252
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
253253
memcpy(code+9, chaincode.begin(), 32);
254-
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
255-
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
254+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
255+
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
256256
}
257257

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

src/pubkey.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,33 @@ class CPubKey
3333
/**
3434
* secp256k1:
3535
*/
36-
static constexpr unsigned int PUBLIC_KEY_SIZE = 65;
37-
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
38-
static constexpr unsigned int SIGNATURE_SIZE = 72;
39-
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
36+
static constexpr unsigned int SIZE = 65;
37+
static constexpr unsigned int COMPRESSED_SIZE = 33;
38+
static constexpr unsigned int SIGNATURE_SIZE = 72;
39+
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
4040
/**
4141
* see www.keylength.com
4242
* script supports up to 75 for single byte push
4343
*/
4444
static_assert(
45-
PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
46-
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
45+
SIZE >= COMPRESSED_SIZE,
46+
"COMPRESSED_SIZE is larger than SIZE");
4747

4848
private:
4949

5050
/**
5151
* Just store the serialized data.
5252
* Its length can very cheaply be computed from the first byte.
5353
*/
54-
unsigned char vch[PUBLIC_KEY_SIZE];
54+
unsigned char vch[SIZE];
5555

5656
//! Compute the length of a pubkey with a given first byte.
5757
unsigned int static GetLen(unsigned char chHeader)
5858
{
5959
if (chHeader == 2 || chHeader == 3)
60-
return COMPRESSED_PUBLIC_KEY_SIZE;
60+
return COMPRESSED_SIZE;
6161
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
62-
return PUBLIC_KEY_SIZE;
62+
return SIZE;
6363
return 0;
6464
}
6565

@@ -140,7 +140,7 @@ class CPubKey
140140
void Unserialize(Stream& s)
141141
{
142142
unsigned int len = ::ReadCompactSize(s);
143-
if (len <= PUBLIC_KEY_SIZE) {
143+
if (len <= SIZE) {
144144
s.read((char*)vch, len);
145145
} else {
146146
// invalid pubkey, skip available data
@@ -179,7 +179,7 @@ class CPubKey
179179
//! Check whether this is a compressed public key.
180180
bool IsCompressed() const
181181
{
182-
return size() == COMPRESSED_PUBLIC_KEY_SIZE;
182+
return size() == COMPRESSED_SIZE;
183183
}
184184

185185
/**

src/script/interpreter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ static inline void popstack(std::vector<valtype>& stack)
6161
}
6262

6363
bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
64-
if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
64+
if (vchPubKey.size() < CPubKey::COMPRESSED_SIZE) {
6565
// Non-canonical public key: too short
6666
return false;
6767
}
6868
if (vchPubKey[0] == 0x04) {
69-
if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) {
69+
if (vchPubKey.size() != CPubKey::SIZE) {
7070
// Non-canonical public key: invalid length for uncompressed key
7171
return false;
7272
}
7373
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
74-
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
74+
if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) {
7575
// Non-canonical public key: invalid length for compressed key
7676
return false;
7777
}
@@ -83,7 +83,7 @@ bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
8383
}
8484

8585
bool static IsCompressedPubKey(const valtype &vchPubKey) {
86-
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
86+
if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) {
8787
// Non-canonical public key: invalid length for compressed key
8888
return false;
8989
}

src/script/sign.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ template<typename Stream>
101101
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
102102
{
103103
// Make sure that the key is the size of pubkey + 1
104-
if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
104+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
105105
throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
106106
}
107107
// Read in the pubkey from key

src/script/standard.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ const char* GetTxnOutputType(txnouttype t)
4444

4545
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
4646
{
47-
if (script.size() == CPubKey::PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
48-
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::PUBLIC_KEY_SIZE + 1);
47+
if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE && script.back() == OP_CHECKSIG) {
48+
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
4949
return CPubKey::ValidSize(pubkey);
5050
}
51-
if (script.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
52-
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1);
51+
if (script.size() == CPubKey::COMPRESSED_SIZE + 2 && script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
52+
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_SIZE + 1);
5353
return CPubKey::ValidSize(pubkey);
5454
}
5555
return false;

0 commit comments

Comments
 (0)