Skip to content

Commit 6a877e8

Browse files
committed
Merge pull request #6034
a574899 chaincodes: abstract away more chaincode behavior [squashme] replace struct CCainCode with a typedef uint256 ChainCode (Cory Fields) 8cf1485 Abstract chaincodes into CChainCode (Pieter Wuille)
2 parents 1fd2d39 + a574899 commit 6a877e8

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

src/hash.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "hash.h"
66
#include "crypto/common.h"
77
#include "crypto/hmac_sha512.h"
8+
#include "pubkey.h"
89

910

1011
inline uint32_t ROTL32(uint32_t x, int8_t r)
@@ -71,15 +72,12 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
7172
return h1;
7273
}
7374

74-
void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
75+
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
7576
{
7677
unsigned char num[4];
7778
num[0] = (nChild >> 24) & 0xFF;
7879
num[1] = (nChild >> 16) & 0xFF;
7980
num[2] = (nChild >> 8) & 0xFF;
8081
num[3] = (nChild >> 0) & 0xFF;
81-
CHMAC_SHA512(chainCode, 32).Write(&header, 1)
82-
.Write(data, 32)
83-
.Write(num, 4)
84-
.Finalize(output);
82+
CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);
8583
}

src/hash.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <vector>
1616

17+
typedef uint256 ChainCode;
18+
1719
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
1820
class CHash256 {
1921
private:
@@ -159,6 +161,6 @@ uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL
159161

160162
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
161163

162-
void BIP32Hash(const unsigned char chainCode[32], unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
164+
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
163165

164166
#endif // BITCOIN_HASH_H

src/key.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
111111
return VerifyPubKey(vchPubKey);
112112
}
113113

114-
bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
114+
bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
115115
assert(IsValid());
116116
assert(IsCompressed());
117117
unsigned char out[64];
@@ -124,7 +124,7 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
124124
assert(begin() + 32 == end());
125125
BIP32Hash(cc, nChild, 0, begin(), out);
126126
}
127-
memcpy(ccChild, out+32, 32);
127+
memcpy(ccChild.begin(), out+32, 32);
128128
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
129129
bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context, (unsigned char*)keyChild.begin(), out);
130130
UnlockObject(out);
@@ -138,7 +138,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int nChild) const {
138138
CKeyID id = key.GetPubKey().GetID();
139139
memcpy(&out.vchFingerprint[0], &id, 4);
140140
out.nChild = nChild;
141-
return key.Derive(out.key, out.vchChainCode, nChild, vchChainCode);
141+
return key.Derive(out.key, out.chaincode, nChild, chaincode);
142142
}
143143

144144
void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
@@ -147,7 +147,7 @@ void CExtKey::SetMaster(const unsigned char *seed, unsigned int nSeedLen) {
147147
LockObject(out);
148148
CHMAC_SHA512(hashkey, sizeof(hashkey)).Write(seed, nSeedLen).Finalize(out);
149149
key.Set(&out[0], &out[32], true);
150-
memcpy(vchChainCode, &out[32], 32);
150+
memcpy(chaincode.begin(), &out[32], 32);
151151
UnlockObject(out);
152152
nDepth = 0;
153153
nChild = 0;
@@ -160,7 +160,7 @@ CExtPubKey CExtKey::Neuter() const {
160160
memcpy(&ret.vchFingerprint[0], &vchFingerprint[0], 4);
161161
ret.nChild = nChild;
162162
ret.pubkey = key.GetPubKey();
163-
memcpy(&ret.vchChainCode[0], &vchChainCode[0], 32);
163+
ret.chaincode = chaincode;
164164
return ret;
165165
}
166166

@@ -169,7 +169,7 @@ void CExtKey::Encode(unsigned char code[74]) const {
169169
memcpy(code+1, vchFingerprint, 4);
170170
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
171171
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
172-
memcpy(code+9, vchChainCode, 32);
172+
memcpy(code+9, chaincode.begin(), 32);
173173
code[41] = 0;
174174
assert(key.size() == 32);
175175
memcpy(code+42, key.begin(), 32);
@@ -179,7 +179,7 @@ void CExtKey::Decode(const unsigned char code[74]) {
179179
nDepth = code[0];
180180
memcpy(vchFingerprint, code+1, 4);
181181
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
182-
memcpy(vchChainCode, code+9, 32);
182+
memcpy(chaincode.begin(), code+9, 32);
183183
key.Set(code+42, code+74, true);
184184
}
185185

src/key.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
#ifndef BITCOIN_KEY_H
77
#define BITCOIN_KEY_H
88

9+
#include "pubkey.h"
910
#include "serialize.h"
1011
#include "support/allocators/secure.h"
1112
#include "uint256.h"
1213

1314
#include <stdexcept>
1415
#include <vector>
1516

16-
class CPubKey;
17-
18-
struct CExtPubKey;
1917

2018
/**
2119
* secp256k1:
@@ -138,7 +136,7 @@ class CKey
138136
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
139137

140138
//! Derive BIP32 child key.
141-
bool Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
139+
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
142140

143141
/**
144142
* Verify thoroughly whether a private key and a public key match.
@@ -157,13 +155,13 @@ struct CExtKey {
157155
unsigned char nDepth;
158156
unsigned char vchFingerprint[4];
159157
unsigned int nChild;
160-
unsigned char vchChainCode[32];
158+
ChainCode chaincode;
161159
CKey key;
162160

163161
friend bool operator==(const CExtKey& a, const CExtKey& b)
164162
{
165163
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
166-
memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.key == b.key;
164+
a.chaincode == b.chaincode && a.key == b.key;
167165
}
168166

169167
void Encode(unsigned char code[74]) const;

src/pubkey.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ bool CPubKey::Decompress() {
5454
return true;
5555
}
5656

57-
bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const {
57+
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
5858
assert(IsValid());
5959
assert((nChild >> 31) == 0);
6060
assert(begin() + 33 == end());
6161
unsigned char out[64];
6262
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
63-
memcpy(ccChild, out+32, 32);
63+
memcpy(ccChild.begin(), out+32, 32);
6464
CECKey key;
6565
bool ret = key.SetPubKey(begin(), size());
6666
ret &= key.TweakPublic(out);
@@ -75,7 +75,7 @@ void CExtPubKey::Encode(unsigned char code[74]) const {
7575
memcpy(code+1, vchFingerprint, 4);
7676
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
7777
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
78-
memcpy(code+9, vchChainCode, 32);
78+
memcpy(code+9, chaincode.begin(), 32);
7979
assert(pubkey.size() == 33);
8080
memcpy(code+41, pubkey.begin(), 33);
8181
}
@@ -84,7 +84,7 @@ void CExtPubKey::Decode(const unsigned char code[74]) {
8484
nDepth = code[0];
8585
memcpy(vchFingerprint, code+1, 4);
8686
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
87-
memcpy(vchChainCode, code+9, 32);
87+
memcpy(chaincode.begin(), code+9, 32);
8888
pubkey.Set(code+41, code+74);
8989
}
9090

@@ -93,5 +93,5 @@ bool CExtPubKey::Derive(CExtPubKey &out, unsigned int nChild) const {
9393
CKeyID id = pubkey.GetID();
9494
memcpy(&out.vchFingerprint[0], &id, 4);
9595
out.nChild = nChild;
96-
return pubkey.Derive(out.pubkey, out.vchChainCode, nChild, vchChainCode);
96+
return pubkey.Derive(out.pubkey, out.chaincode, nChild, chaincode);
9797
}

src/pubkey.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class CKeyID : public uint160
3131
CKeyID(const uint160& in) : uint160(in) {}
3232
};
3333

34+
typedef uint256 ChainCode;
35+
3436
/** An encapsulated public key. */
3537
class CPubKey
3638
{
@@ -182,20 +184,20 @@ class CPubKey
182184
bool Decompress();
183185

184186
//! Derive BIP32 child pubkey.
185-
bool Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
187+
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
186188
};
187189

188190
struct CExtPubKey {
189191
unsigned char nDepth;
190192
unsigned char vchFingerprint[4];
191193
unsigned int nChild;
192-
unsigned char vchChainCode[32];
194+
ChainCode chaincode;
193195
CPubKey pubkey;
194196

195-
friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
197+
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
196198
{
197199
return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
198-
memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.pubkey == b.pubkey;
200+
a.chaincode == b.chaincode && a.pubkey == b.pubkey;
199201
}
200202

201203
void Encode(unsigned char code[74]) const;

0 commit comments

Comments
 (0)