Skip to content

Commit 81b8eb4

Browse files
laanwjUdjinM6
authored andcommitted
Merge bitcoin#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@0580f86 Tree-SHA512: 29aa0be54912358b138e391b9db78639786f56580493e590ec9f773c0e1b421740133d05a79be247c7ee57e71c9c9e41b9cb54088cb3c0e3f813f74f0895287b
1 parent c4f7bb5 commit 81b8eb4

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
@@ -84,13 +84,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
8484
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
8585
* included.
8686
*
87-
* privkey must point to an output buffer of length at least CKey::PRIVATE_KEY_SIZE bytes.
87+
* privkey must point to an output buffer of length at least CKey::SIZE bytes.
8888
* privkeylen must initially be set to the size of the privkey buffer. Upon return it
8989
* will be set to the number of bytes used in the buffer.
9090
* key32 must point to a 32-byte raw private key.
9191
*/
9292
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, bool compressed) {
93-
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
93+
assert(*privkeylen >= CKey::SIZE);
9494
secp256k1_pubkey pubkey;
9595
size_t pubkeylen = 0;
9696
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
@@ -116,11 +116,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
116116
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
117117
memcpy(ptr, key32, 32); ptr += 32;
118118
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
119-
pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE;
119+
pubkeylen = CPubKey::COMPRESSED_SIZE;
120120
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
121121
ptr += pubkeylen;
122122
*privkeylen = ptr - privkey;
123-
assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
123+
assert(*privkeylen == CKey::COMPRESSED_SIZE);
124124
} else {
125125
static const unsigned char begin[] = {
126126
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
@@ -142,11 +142,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
142142
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
143143
memcpy(ptr, key32, 32); ptr += 32;
144144
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
145-
pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
145+
pubkeylen = CPubKey::SIZE;
146146
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
147147
ptr += pubkeylen;
148148
*privkeylen = ptr - privkey;
149-
assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
149+
assert(*privkeylen == CKey::SIZE);
150150
}
151151
return 1;
152152
}
@@ -174,8 +174,8 @@ CPrivKey CKey::GetPrivKey() const {
174174
CPrivKey privkey;
175175
int ret;
176176
size_t privkeylen;
177-
privkey.resize(PRIVATE_KEY_SIZE);
178-
privkeylen = PRIVATE_KEY_SIZE;
177+
privkey.resize(SIZE);
178+
privkeylen = SIZE;
179179
ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
180180
assert(ret);
181181
privkey.resize(privkeylen);
@@ -185,7 +185,7 @@ CPrivKey CKey::GetPrivKey() const {
185185
CPubKey CKey::GetPubKey() const {
186186
assert(fValid);
187187
secp256k1_pubkey pubkey;
188-
size_t clen = CPubKey::PUBLIC_KEY_SIZE;
188+
size_t clen = CPubKey::SIZE;
189189
CPubKey result;
190190
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
191191
assert(ret);
@@ -277,7 +277,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
277277
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
278278
if ((nChild >> 31) == 0) {
279279
CPubKey pubkey = GetPubKey();
280-
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
280+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
281281
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
282282
} else {
283283
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
@@ -130,7 +130,7 @@ struct PSBTInput
130130
case PSBT_IN_PARTIAL_SIG:
131131
{
132132
// Make sure that the key is the size of pubkey + 1
133-
if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
133+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
134134
throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey");
135135
}
136136
// Read in the pubkey from key

src/pubkey.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
198198
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
199199
return false;
200200
}
201-
unsigned char pub[PUBLIC_KEY_SIZE];
202-
size_t publen = PUBLIC_KEY_SIZE;
201+
unsigned char pub[SIZE];
202+
size_t publen = SIZE;
203203
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
204204
Set(pub, pub + publen);
205205
return true;
@@ -221,8 +221,8 @@ bool CPubKey::Decompress() {
221221
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
222222
return false;
223223
}
224-
unsigned char pub[PUBLIC_KEY_SIZE];
225-
size_t publen = PUBLIC_KEY_SIZE;
224+
unsigned char pub[SIZE];
225+
size_t publen = SIZE;
226226
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
227227
Set(pub, pub + publen);
228228
return true;
@@ -231,7 +231,7 @@ bool CPubKey::Decompress() {
231231
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
232232
assert(IsValid());
233233
assert((nChild >> 31) == 0);
234-
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE);
234+
assert(size() == COMPRESSED_SIZE);
235235
unsigned char out[64];
236236
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
237237
memcpy(ccChild.begin(), out+32, 32);
@@ -243,8 +243,8 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
243243
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
244244
return false;
245245
}
246-
unsigned char pub[COMPRESSED_PUBLIC_KEY_SIZE];
247-
size_t publen = COMPRESSED_PUBLIC_KEY_SIZE;
246+
unsigned char pub[COMPRESSED_SIZE];
247+
size_t publen = COMPRESSED_SIZE;
248248
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
249249
pubkeyChild.Set(pub, pub + publen);
250250
return true;
@@ -256,8 +256,8 @@ void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
256256
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
257257
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
258258
memcpy(code+9, chaincode.begin(), 32);
259-
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
260-
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
259+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
260+
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
261261
}
262262

263263
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
@@ -145,7 +145,7 @@ template<typename Stream>
145145
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
146146
{
147147
// Make sure that the key is the size of pubkey + 1
148-
if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
148+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
149149
throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
150150
}
151151
// Read in the pubkey from key

src/script/standard.cpp

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

3535
static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
3636
{
37-
if (script.size() == CPubKey::PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
38-
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::PUBLIC_KEY_SIZE + 1);
37+
if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE && script.back() == OP_CHECKSIG) {
38+
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
3939
return CPubKey::ValidSize(pubkey);
4040
}
41-
if (script.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
42-
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1);
41+
if (script.size() == CPubKey::COMPRESSED_SIZE + 2 && script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
42+
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_SIZE + 1);
4343
return CPubKey::ValidSize(pubkey);
4444
}
4545
return false;

0 commit comments

Comments
 (0)