Browse Source

Use libsecp256k1 in key.cpp

0.10
Pieter Wuille 10 years ago
parent
commit
dffb8f81b8
  1. 71
      src/key.cpp

71
src/key.cpp

@ -9,21 +9,16 @@ @@ -9,21 +9,16 @@
#include "pubkey.h"
#include "random.h"
#ifdef USE_SECP256K1
#include <secp256k1.h>
#else
#include "ecwrapper.h"
#endif
//! anonymous namespace
namespace {
#ifdef USE_SECP256K1
#include <secp256k1.h>
class CSecp256k1Init {
public:
CSecp256k1Init() {
secp256k1_start();
secp256k1_start(SECP256K1_START_SIGN);
}
~CSecp256k1Init() {
secp256k1_stop();
@ -31,7 +26,6 @@ public: @@ -31,7 +26,6 @@ public:
};
static CSecp256k1Init instance_of_csecp256k1;
#endif
} // anon namespace
bool CKey::Check(const unsigned char *vch) {
@ -47,15 +41,8 @@ void CKey::MakeNewKey(bool fCompressedIn) { @@ -47,15 +41,8 @@ void CKey::MakeNewKey(bool fCompressedIn) {
}
bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
#ifdef USE_SECP256K1
if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
return false;
#else
CECKey key;
if (!key.SetPrivKey(&privkey[0], privkey.size()))
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
return false;
key.GetSecretBytes(vch);
#endif
fCompressed = fCompressedIn;
fValid = true;
return true;
@ -65,39 +52,21 @@ CPrivKey CKey::GetPrivKey() const { @@ -65,39 +52,21 @@ CPrivKey CKey::GetPrivKey() const {
assert(fValid);
CPrivKey privkey;
int privkeylen, ret;
#ifdef USE_SECP256K1
privkey.resize(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);
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;
}
CPubKey CKey::GetPubKey() const {
assert(fValid);
CPubKey result;
#ifdef USE_SECP256K1
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(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());
return result;
}
@ -105,7 +74,6 @@ CPubKey CKey::GetPubKey() const { @@ -105,7 +74,6 @@ CPubKey CKey::GetPubKey() const {
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
if (!fValid)
return false;
#ifdef USE_SECP256K1
vchSig.resize(72);
int nSigLen = 72;
CKey nonce;
@ -116,11 +84,6 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const { @@ -116,11 +84,6 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
} while(true);
vchSig.resize(nSigLen);
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 {
@ -128,34 +91,20 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) @@ -128,34 +91,20 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
return false;
vchSig.resize(65);
int rec = -1;
#ifdef USE_SECP256K1
CKey nonce;
do {
nonce.MakeNewKey(true);
if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
break;
} while(true);
#else
CECKey key;
key.SetSecretBytes(vch);
if (!key.SignCompact(hash, &vchSig[1], rec))
return false;
#endif
assert(rec != -1);
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
return true;
}
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
#ifdef USE_SECP256K1
if (!secp256k1_ecdsa_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
return false;
#else
CECKey key;
if (!key.SetPrivKey(&privkey[0], privkey.size(), fSkipCheck))
if (!secp256k1_ec_privkey_import((unsigned char*)begin(), &privkey[0], privkey.size()))
return false;
key.GetSecretBytes(vch);
#endif
fCompressed = vchPubKey.IsCompressed();
fValid = true;
@ -182,12 +131,8 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild @@ -182,12 +131,8 @@ bool CKey::Derive(CKey& keyChild, unsigned char ccChild[32], unsigned int nChild
BIP32Hash(cc, nChild, 0, begin(), out);
}
memcpy(ccChild, out+32, 32);
#ifdef USE_SECP256K1
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
bool ret = secp256k1_ecdsa_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
#else
bool ret = CECKey::TweakSecret((unsigned char*)keyChild.begin(), begin(), out);
#endif
bool ret = secp256k1_ec_privkey_tweak_add((unsigned char*)keyChild.begin(), out);
UnlockObject(out);
keyChild.fCompressed = true;
keyChild.fValid = ret;
@ -245,9 +190,5 @@ void CExtKey::Decode(const unsigned char code[74]) { @@ -245,9 +190,5 @@ void CExtKey::Decode(const unsigned char code[74]) {
}
bool ECC_InitSanityCheck() {
#ifdef USE_SECP256K1
return true;
#else
return CECKey::SanityCheck();
#endif
}

Loading…
Cancel
Save