mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
Use libsecp256k1 in key.cpp
This commit is contained in:
parent
4c97c64bf6
commit
dffb8f81b8
71
src/key.cpp
71
src/key.cpp
@ -9,21 +9,16 @@
|
|||||||
#include "pubkey.h"
|
#include "pubkey.h"
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
|
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
#include <secp256k1.h>
|
#include <secp256k1.h>
|
||||||
#else
|
|
||||||
#include "ecwrapper.h"
|
#include "ecwrapper.h"
|
||||||
#endif
|
|
||||||
|
|
||||||
//! anonymous namespace
|
//! anonymous namespace
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
#include <secp256k1.h>
|
|
||||||
class CSecp256k1Init {
|
class CSecp256k1Init {
|
||||||
public:
|
public:
|
||||||
CSecp256k1Init() {
|
CSecp256k1Init() {
|
||||||
secp256k1_start();
|
secp256k1_start(SECP256K1_START_SIGN);
|
||||||
}
|
}
|
||||||
~CSecp256k1Init() {
|
~CSecp256k1Init() {
|
||||||
secp256k1_stop();
|
secp256k1_stop();
|
||||||
@ -31,7 +26,6 @@ public:
|
|||||||
};
|
};
|
||||||
static CSecp256k1Init instance_of_csecp256k1;
|
static CSecp256k1Init instance_of_csecp256k1;
|
||||||
|
|
||||||
#endif
|
|
||||||
} // anon namespace
|
} // anon namespace
|
||||||
|
|
||||||
bool CKey::Check(const unsigned char *vch) {
|
bool CKey::Check(const unsigned char *vch) {
|
||||||
@ -47,15 +41,8 @@ void CKey::MakeNewKey(bool fCompressedIn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
|
bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
|
||||||
#ifdef USE_SECP256K1
|
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||||
if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
|
||||||
return false;
|
return false;
|
||||||
#else
|
|
||||||
CECKey key;
|
|
||||||
if (!key.SetPrivKey(&privkey[0], privkey.size()))
|
|
||||||
return false;
|
|
||||||
key.GetSecretBytes(vch);
|
|
||||||
#endif
|
|
||||||
fCompressed = fCompressedIn;
|
fCompressed = fCompressedIn;
|
||||||
fValid = true;
|
fValid = true;
|
||||||
return true;
|
return true;
|
||||||
@ -65,39 +52,21 @@ CPrivKey CKey::GetPrivKey() const {
|
|||||||
assert(fValid);
|
assert(fValid);
|
||||||
CPrivKey privkey;
|
CPrivKey privkey;
|
||||||
int privkeylen, ret;
|
int privkeylen, ret;
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
privkey.resize(279);
|
privkey.resize(279);
|
||||||
privkeylen = 279;
|
privkeylen = 279;
|
||||||
ret = secp256k1_ecdsa_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
|
ret = secp256k1_ec_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
|
||||||
assert(ret);
|
assert(ret);
|
||||||
privkey.resize(privkeylen);
|
privkey.resize(privkeylen);
|
||||||
#else
|
|
||||||
CECKey key;
|
|
||||||
key.SetSecretBytes(vch);
|
|
||||||
privkeylen = key.GetPrivKeySize(fCompressed);
|
|
||||||
assert(privkeylen);
|
|
||||||
privkey.resize(privkeylen);
|
|
||||||
ret = key.GetPrivKey(&privkey[0], fCompressed);
|
|
||||||
assert(ret == (int)privkey.size());
|
|
||||||
#endif
|
|
||||||
return privkey;
|
return privkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
CPubKey CKey::GetPubKey() const {
|
CPubKey CKey::GetPubKey() const {
|
||||||
assert(fValid);
|
assert(fValid);
|
||||||
CPubKey result;
|
CPubKey result;
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
int clen = 65;
|
int clen = 65;
|
||||||
int ret = secp256k1_ecdsa_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
|
int ret = secp256k1_ec_pubkey_create((unsigned char*)result.begin(), &clen, begin(), fCompressed);
|
||||||
assert((int)result.size() == clen);
|
assert((int)result.size() == clen);
|
||||||
assert(ret);
|
assert(ret);
|
||||||
#else
|
|
||||||
std::vector<unsigned char> pubkey;
|
|
||||||
CECKey key;
|
|
||||||
key.SetSecretBytes(vch);
|
|
||||||
key.GetPubKey(pubkey, fCompressed);
|
|
||||||
result.Set(pubkey.begin(), pubkey.end());
|
|
||||||
#endif
|
|
||||||
assert(result.IsValid());
|
assert(result.IsValid());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -105,7 +74,6 @@ CPubKey CKey::GetPubKey() const {
|
|||||||
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
||||||
if (!fValid)
|
if (!fValid)
|
||||||
return false;
|
return false;
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
vchSig.resize(72);
|
vchSig.resize(72);
|
||||||
int nSigLen = 72;
|
int nSigLen = 72;
|
||||||
CKey nonce;
|
CKey nonce;
|
||||||
@ -116,11 +84,6 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
|||||||
} while(true);
|
} while(true);
|
||||||
vchSig.resize(nSigLen);
|
vchSig.resize(nSigLen);
|
||||||
return true;
|
return true;
|
||||||
#else
|
|
||||||
CECKey key;
|
|
||||||
key.SetSecretBytes(vch);
|
|
||||||
return key.Sign(hash, vchSig);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
|
||||||
@ -128,34 +91,20 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
|
|||||||
return false;
|
return false;
|
||||||
vchSig.resize(65);
|
vchSig.resize(65);
|
||||||
int rec = -1;
|
int rec = -1;
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
CKey nonce;
|
CKey nonce;
|
||||||
do {
|
do {
|
||||||
nonce.MakeNewKey(true);
|
nonce.MakeNewKey(true);
|
||||||
if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
|
if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
|
||||||
break;
|
break;
|
||||||
} while(true);
|
} while(true);
|
||||||
#else
|
|
||||||
CECKey key;
|
|
||||||
key.SetSecretBytes(vch);
|
|
||||||
if (!key.SignCompact(hash, &vchSig[1], rec))
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
assert(rec != -1);
|
assert(rec != -1);
|
||||||
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
|
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
|
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
|
||||||
#ifdef USE_SECP256K1
|
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
||||||
if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
|
|
||||||
return false;
|
return false;
|
||||||
#else
|
|
||||||
CECKey key;
|
|
||||||
if (!key.SetPrivKey(&privkey[0], privkey.size(), fSkipCheck))
|
|
||||||
return false;
|
|
||||||
key.GetSecretBytes(vch);
|
|
||||||
#endif
|
|
||||||
fCompressed = vchPubKey.IsCompressed();
|
fCompressed = vchPubKey.IsCompressed();
|
||||||
fValid = true;
|
fValid = true;
|
||||||
|
|
||||||
@ -182,12 +131,8 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
|
|||||||
BIP32Hash(cc, nChild, 0, begin(), out);
|
BIP32Hash(cc, nChild, 0, begin(), out);
|
||||||
}
|
}
|
||||||
memcpy(ccChild, out+32, 32);
|
memcpy(ccChild, out+32, 32);
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
|
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
|
||||||
bool ret = secp256k1_ecdsa_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
|
bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
|
||||||
#else
|
|
||||||
bool ret = CECKey::TweakSecret((unsigned char*)keyChild.begin(), begin(), out);
|
|
||||||
#endif
|
|
||||||
UnlockObject(out);
|
UnlockObject(out);
|
||||||
keyChild.fCompressed = true;
|
keyChild.fCompressed = true;
|
||||||
keyChild.fValid = ret;
|
keyChild.fValid = ret;
|
||||||
@ -245,9 +190,5 @@ void CExtKey::Decode(const unsigned char code[74]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ECC_InitSanityCheck() {
|
bool ECC_InitSanityCheck() {
|
||||||
#ifdef USE_SECP256K1
|
|
||||||
return true;
|
|
||||||
#else
|
|
||||||
return CECKey::SanityCheck();
|
return CECKey::SanityCheck();
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user