1 // Copyright (c) 2009-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "eccryptoverify.h"
11 bool CPubKey::Verify(const uint256
&hash
, const std::vector
<unsigned char>& vchSig
) const {
15 if (!key
.SetPubKey(begin(), size()))
17 if (!key
.Verify(hash
, vchSig
))
22 bool CPubKey::RecoverCompact(const uint256
&hash
, const std::vector
<unsigned char>& vchSig
) {
23 if (vchSig
.size() != 65)
25 int recid
= (vchSig
[0] - 27) & 3;
26 bool fComp
= ((vchSig
[0] - 27) & 4) != 0;
28 if (!key
.Recover(hash
, &vchSig
[1], recid
))
30 std::vector
<unsigned char> pubkey
;
31 key
.GetPubKey(pubkey
, fComp
);
32 Set(pubkey
.begin(), pubkey
.end());
36 bool CPubKey::IsFullyValid() const {
40 if (!key
.SetPubKey(begin(), size()))
45 bool CPubKey::Decompress() {
49 if (!key
.SetPubKey(begin(), size()))
51 std::vector
<unsigned char> pubkey
;
52 key
.GetPubKey(pubkey
, false);
53 Set(pubkey
.begin(), pubkey
.end());
57 bool CPubKey::Derive(CPubKey
& pubkeyChild
, ChainCode
&ccChild
, unsigned int nChild
, const ChainCode
& cc
) const {
59 assert((nChild
>> 31) == 0);
60 assert(begin() + 33 == end());
61 unsigned char out
[64];
62 BIP32Hash(cc
, nChild
, *begin(), begin()+1, out
);
63 memcpy(ccChild
.begin(), out
+32, 32);
65 bool ret
= key
.SetPubKey(begin(), size());
66 ret
&= key
.TweakPublic(out
);
67 std::vector
<unsigned char> pubkey
;
68 key
.GetPubKey(pubkey
, true);
69 pubkeyChild
.Set(pubkey
.begin(), pubkey
.end());
73 void CExtPubKey::Encode(unsigned char code
[74]) const {
75 memcpy(code
+1, vchFingerprint
, 4);
76 code
[5] = (nChild
>> 24) & 0xFF; code
[6] = (nChild
>> 16) & 0xFF;
77 code
[7] = (nChild
>> 8) & 0xFF; code
[8] = (nChild
>> 0) & 0xFF;
78 memcpy(code
+9, chaincode
.begin(), 32);
79 assert(pubkey
.size() == 33);
80 memcpy(code
+41, pubkey
.begin(), 33);
83 void CExtPubKey::Decode(const unsigned char code
[74]) {
85 memcpy(vchFingerprint
, code
+1, 4);
86 nChild
= (code
[5] << 24) | (code
[6] << 16) | (code
[7] << 8) | code
[8];
87 memcpy(chaincode
.begin(), code
+9, 32);
88 pubkey
.Set(code
+41, code
+74);
91 bool CExtPubKey::Derive(CExtPubKey
&out
, unsigned int nChild
) const {
92 out
.nDepth
= nDepth
+ 1;
93 CKeyID id
= pubkey
.GetID();
94 memcpy(&out
.vchFingerprint
[0], &id
, 4);
96 return pubkey
.Derive(out
.pubkey
, out
.chaincode
, nChild
, chaincode
);