Navigation Menu

Skip to content

Commit

Permalink
Merge #8321: [trivial] Switched constants to sizeof()
Browse files Browse the repository at this point in the history
fbc6070 [trivial] Switched constants to sizeof() (Thomas Snider)
  • Loading branch information
laanwj committed Jul 28, 2016
2 parents 2266b43 + fbc6070 commit 133c727
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
28 changes: 16 additions & 12 deletions src/key.h
Expand Up @@ -15,7 +15,7 @@
#include <vector>


/**
/**
* secp256k1:
* const unsigned int PRIVATE_KEY_SIZE = 279;
* const unsigned int PUBLIC_KEY_SIZE = 65;
Expand Down Expand Up @@ -45,6 +45,8 @@ class CKey
//! The actual byte data
unsigned char vch[32];

static_assert(sizeof(vch) == 32, "vch must be 32 bytes in length to not break serialization");

//! Check whether the 32-byte array pointed to be vch is valid keydata.
bool static Check(const unsigned char* vch);

Expand All @@ -70,20 +72,19 @@ class CKey

friend bool operator==(const CKey& a, const CKey& b)
{
return a.fCompressed == b.fCompressed && a.size() == b.size() &&
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
return a.fCompressed == b.fCompressed &&
a.size() == b.size() &&
memcmp(&a.vch[0], &b.vch[0], a.size()) == 0;
}

//! Initialize using begin and end iterators to byte data.
template <typename T>
void Set(const T pbegin, const T pend, bool fCompressedIn)
{
if (pend - pbegin != 32) {
if (pend - pbegin != sizeof(vch)) {
fValid = false;
return;
}
if (Check(&pbegin[0])) {
memcpy(vch, (unsigned char*)&pbegin[0], 32);
} else if (Check(&pbegin[0])) {
memcpy(vch, (unsigned char*)&pbegin[0], sizeof(vch));
fValid = true;
fCompressed = fCompressedIn;
} else {
Expand All @@ -92,7 +93,7 @@ class CKey
}

//! Simple read-only vector-like interface.
unsigned int size() const { return (fValid ? 32 : 0); }
unsigned int size() const { return (fValid ? sizeof(vch) : 0); }
const unsigned char* begin() const { return vch; }
const unsigned char* end() const { return vch + size(); }

Expand All @@ -110,7 +111,7 @@ class CKey

/**
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
* This is expensive.
* This is expensive.
*/
CPrivKey GetPrivKey() const;

Expand Down Expand Up @@ -157,8 +158,11 @@ struct CExtKey {

friend bool operator==(const CExtKey& a, const CExtKey& b)
{
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
a.chaincode == b.chaincode && a.key == b.key;
return a.nDepth == b.nDepth &&
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
a.nChild == b.nChild &&
a.chaincode == b.chaincode &&
a.key == b.key;
}

void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
Expand Down
11 changes: 7 additions & 4 deletions src/pubkey.h
Expand Up @@ -13,7 +13,7 @@
#include <stdexcept>
#include <vector>

/**
/**
* secp256k1:
* const unsigned int PRIVATE_KEY_SIZE = 279;
* const unsigned int PUBLIC_KEY_SIZE = 65;
Expand Down Expand Up @@ -156,7 +156,7 @@ class CPubKey

/*
* Check syntactic correctness.
*
*
* Note that this is consensus critical as CheckSig() calls it!
*/
bool IsValid() const
Expand Down Expand Up @@ -203,8 +203,11 @@ struct CExtPubKey {

friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
{
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
a.chaincode == b.chaincode && a.pubkey == b.pubkey;
return a.nDepth == b.nDepth &&
memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
a.nChild == b.nChild &&
a.chaincode == b.chaincode &&
a.pubkey == b.pubkey;
}

void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
Expand Down

0 comments on commit 133c727

Please sign in to comment.