|
|
@ -6,25 +6,16 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "eccryptoverify.h" |
|
|
|
#include "eccryptoverify.h" |
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
#include <secp256k1.h> |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
#include "ecwrapper.h" |
|
|
|
#include "ecwrapper.h" |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const { |
|
|
|
bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const { |
|
|
|
if (!IsValid()) |
|
|
|
if (!IsValid()) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
if (secp256k1_ecdsa_verify((const unsigned char*)&hash, &vchSig[0], vchSig.size(), begin(), size()) != 1) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
CECKey key; |
|
|
|
CECKey key; |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
if (!key.Verify(hash, vchSig)) |
|
|
|
if (!key.Verify(hash, vchSig)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
#endif |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -33,52 +24,33 @@ bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned cha |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
int recid = (vchSig[0] - 27) & 3; |
|
|
|
int recid = (vchSig[0] - 27) & 3; |
|
|
|
bool fComp = ((vchSig[0] - 27) & 4) != 0; |
|
|
|
bool fComp = ((vchSig[0] - 27) & 4) != 0; |
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
int pubkeylen = 65; |
|
|
|
|
|
|
|
if (!secp256k1_ecdsa_recover_compact((const unsigned char*)&hash, &vchSig[1], (unsigned char*)begin(), &pubkeylen, fComp, recid)) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
assert((int)size() == pubkeylen); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
CECKey key; |
|
|
|
CECKey key; |
|
|
|
if (!key.Recover(hash, &vchSig[1], recid)) |
|
|
|
if (!key.Recover(hash, &vchSig[1], recid)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
key.GetPubKey(pubkey, fComp); |
|
|
|
key.GetPubKey(pubkey, fComp); |
|
|
|
Set(pubkey.begin(), pubkey.end()); |
|
|
|
Set(pubkey.begin(), pubkey.end()); |
|
|
|
#endif |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CPubKey::IsFullyValid() const { |
|
|
|
bool CPubKey::IsFullyValid() const { |
|
|
|
if (!IsValid()) |
|
|
|
if (!IsValid()) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
if (!secp256k1_ecdsa_pubkey_verify(begin(), size())) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
CECKey key; |
|
|
|
CECKey key; |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
#endif |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CPubKey::Decompress() { |
|
|
|
bool CPubKey::Decompress() { |
|
|
|
if (!IsValid()) |
|
|
|
if (!IsValid()) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
int clen = size(); |
|
|
|
|
|
|
|
int ret = secp256k1_ecdsa_pubkey_decompress((unsigned char*)begin(), &clen); |
|
|
|
|
|
|
|
assert(ret); |
|
|
|
|
|
|
|
assert(clen == (int)size()); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
CECKey key; |
|
|
|
CECKey key; |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
if (!key.SetPubKey(begin(), size())) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
key.GetPubKey(pubkey, false); |
|
|
|
key.GetPubKey(pubkey, false); |
|
|
|
Set(pubkey.begin(), pubkey.end()); |
|
|
|
Set(pubkey.begin(), pubkey.end()); |
|
|
|
#endif |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -89,17 +61,12 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned i |
|
|
|
unsigned char out[64]; |
|
|
|
unsigned char out[64]; |
|
|
|
BIP32Hash(cc, nChild, *begin(), begin()+1, out); |
|
|
|
BIP32Hash(cc, nChild, *begin(), begin()+1, out); |
|
|
|
memcpy(ccChild, out+32, 32); |
|
|
|
memcpy(ccChild, out+32, 32); |
|
|
|
#ifdef USE_SECP256K1 |
|
|
|
|
|
|
|
pubkeyChild = *this; |
|
|
|
|
|
|
|
bool ret = secp256k1_ecdsa_pubkey_tweak_add((unsigned char*)pubkeyChild.begin(), pubkeyChild.size(), out); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
CECKey key; |
|
|
|
CECKey key; |
|
|
|
bool ret = key.SetPubKey(begin(), size()); |
|
|
|
bool ret = key.SetPubKey(begin(), size()); |
|
|
|
ret &= key.TweakPublic(out); |
|
|
|
ret &= key.TweakPublic(out); |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
std::vector<unsigned char> pubkey; |
|
|
|
key.GetPubKey(pubkey, true); |
|
|
|
key.GetPubKey(pubkey, true); |
|
|
|
pubkeyChild.Set(pubkey.begin(), pubkey.end()); |
|
|
|
pubkeyChild.Set(pubkey.begin(), pubkey.end()); |
|
|
|
#endif |
|
|
|
|
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|