Skip to content

Commit a1a5dbf

Browse files
laanwjgades
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 f3723a6 commit a1a5dbf

File tree

8 files changed

+45
-45
lines changed

8 files changed

+45
-45
lines changed

src/key.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -85,13 +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 CKey::PRIVATE_KEY_SIZE bytes.
88+
* privkey must point to an output buffer of length at least CKey::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, bool compressed) {
94-
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
94+
assert(*privkeylen >= CKey::SIZE);
9595
secp256k1_pubkey pubkey;
9696
size_t pubkeylen = 0;
9797
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
@@ -117,11 +117,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
117117
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
118118
memcpy(ptr, key32, 32); ptr += 32;
119119
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
120-
pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE;
120+
pubkeylen = CPubKey::COMPRESSED_SIZE;
121121
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
122122
ptr += pubkeylen;
123123
*privkeylen = ptr - privkey;
124-
assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
124+
assert(*privkeylen == CKey::COMPRESSED_SIZE);
125125
} else {
126126
static const unsigned char begin[] = {
127127
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
@@ -143,11 +143,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
143143
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
144144
memcpy(ptr, key32, 32); ptr += 32;
145145
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
146-
pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
146+
pubkeylen = CPubKey::SIZE;
147147
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
148148
ptr += pubkeylen;
149149
*privkeylen = ptr - privkey;
150-
assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
150+
assert(*privkeylen == CKey::SIZE);
151151
}
152152
return 1;
153153
}
@@ -169,8 +169,8 @@ CPrivKey CKey::GetPrivKey() const {
169169
CPrivKey privkey;
170170
int ret;
171171
size_t privkeylen;
172-
privkey.resize(PRIVATE_KEY_SIZE);
173-
privkeylen = PRIVATE_KEY_SIZE;
172+
privkey.resize(SIZE);
173+
privkeylen = SIZE;
174174
ret = ec_privkey_export_der(secp256k1_context_sign, privkey.data(), &privkeylen, begin(), fCompressed);
175175
assert(ret);
176176
privkey.resize(privkeylen);
@@ -180,7 +180,7 @@ CPrivKey CKey::GetPrivKey() const {
180180
CPubKey CKey::GetPubKey() const {
181181
assert(fValid);
182182
secp256k1_pubkey pubkey;
183-
size_t clen = CPubKey::PUBLIC_KEY_SIZE;
183+
size_t clen = CPubKey::SIZE;
184184
CPubKey result;
185185
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
186186
assert(ret);
@@ -272,7 +272,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
272272
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
273273
if ((nChild >> 31) == 0) {
274274
CPubKey pubkey = GetPubKey();
275-
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
275+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
276276
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
277277
} else {
278278
assert(size() == 32);

src/key.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* secure_allocator is defined in allocators.h
2222
* CPrivKey is a serialized private key, with all parameters included
23-
* (PRIVATE_KEY_SIZE bytes)
23+
* (SIZE bytes)
2424
*/
2525
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
2626

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

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

src/psbt.h

+1-1
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

+9-9
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha
197197
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, hash.begin())) {
198198
return false;
199199
}
200-
unsigned char pub[PUBLIC_KEY_SIZE];
201-
size_t publen = PUBLIC_KEY_SIZE;
200+
unsigned char pub[SIZE];
201+
size_t publen = SIZE;
202202
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
203203
Set(pub, pub + publen);
204204
return true;
@@ -218,8 +218,8 @@ bool CPubKey::Decompress() {
218218
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
219219
return false;
220220
}
221-
unsigned char pub[PUBLIC_KEY_SIZE];
222-
size_t publen = PUBLIC_KEY_SIZE;
221+
unsigned char pub[SIZE];
222+
size_t publen = SIZE;
223223
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
224224
Set(pub, pub + publen);
225225
return true;
@@ -228,7 +228,7 @@ bool CPubKey::Decompress() {
228228
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
229229
assert(IsValid());
230230
assert((nChild >> 31) == 0);
231-
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE);
231+
assert(size() == COMPRESSED_SIZE);
232232
unsigned char out[64];
233233
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
234234
memcpy(ccChild.begin(), out+32, 32);
@@ -239,8 +239,8 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi
239239
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, out)) {
240240
return false;
241241
}
242-
unsigned char pub[COMPRESSED_PUBLIC_KEY_SIZE];
243-
size_t publen = COMPRESSED_PUBLIC_KEY_SIZE;
242+
unsigned char pub[COMPRESSED_SIZE];
243+
size_t publen = COMPRESSED_SIZE;
244244
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
245245
pubkeyChild.Set(pub, pub + publen);
246246
return true;
@@ -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() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
256-
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
255+
assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
256+
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
257257
}
258258

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

src/pubkey.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@ class CPubKey
3434
/**
3535
* secp256k1:
3636
*/
37-
static constexpr unsigned int PUBLIC_KEY_SIZE = 65;
38-
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
39-
static constexpr unsigned int SIGNATURE_SIZE = 72;
40-
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
37+
static constexpr unsigned int SIZE = 65;
38+
static constexpr unsigned int COMPRESSED_SIZE = 33;
39+
static constexpr unsigned int SIGNATURE_SIZE = 72;
40+
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
4141
/**
4242
* see www.keylength.com
4343
* script supports up to 75 for single byte push
4444
*/
4545
static_assert(
46-
PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
47-
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
46+
SIZE >= COMPRESSED_SIZE,
47+
"COMPRESSED_SIZE is larger than SIZE");
4848

4949
private:
5050

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

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

@@ -141,7 +141,7 @@ class CPubKey
141141
void Unserialize(Stream& s)
142142
{
143143
unsigned int len = ::ReadCompactSize(s);
144-
if (len <= PUBLIC_KEY_SIZE) {
144+
if (len <= SIZE) {
145145
s.read((char*)vch, len);
146146
} else {
147147
// invalid pubkey, skip available data
@@ -180,7 +180,7 @@ class CPubKey
180180
//! Check whether this is a compressed public key.
181181
bool IsCompressed() const
182182
{
183-
return size() == COMPRESSED_PUBLIC_KEY_SIZE;
183+
return size() == COMPRESSED_SIZE;
184184
}
185185

186186
/**

src/script/interpreter.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ static inline void popstack(std::vector<valtype>& stack)
6262
}
6363

6464
bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
65-
if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
65+
if (vchPubKey.size() < CPubKey::COMPRESSED_SIZE) {
6666
// Non-canonical public key: too short
6767
return false;
6868
}
6969
if (vchPubKey[0] == 0x04) {
70-
if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) {
70+
if (vchPubKey.size() != CPubKey::SIZE) {
7171
// Non-canonical public key: invalid length for uncompressed key
7272
return false;
7373
}
7474
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
75-
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
75+
if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) {
7676
// Non-canonical public key: invalid length for compressed key
7777
return false;
7878
}
@@ -84,7 +84,7 @@ bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
8484
}
8585

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

src/script/sign.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ template<typename Stream>
146146
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
147147
{
148148
// Make sure that the key is the size of pubkey + 1
149-
if (key.size() != CPubKey::PUBLIC_KEY_SIZE + 1 && key.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1) {
149+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
150150
throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
151151
}
152152
// Read in the pubkey from key

src/script/standard.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ const char* GetTxnOutputType(txnouttype t)
3535

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

0 commit comments

Comments
 (0)