aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2014-11-05 09:47:06 -0800
committerPieter Wuille <pieter.wuille@gmail.com>2014-11-18 18:06:46 +0100
commitdffb8f81b83e1a10100365c696d6a04fc6344728 (patch)
tree8edb9e5d53972353f8c3a400f53f8004f88bbc0c /src
parent4c97c64bf68a8e57c84fc00a97d16723cf828a0a (diff)
Use libsecp256k1 in key.cpp
Diffstat (limited to 'src')
-rw-r--r--src/key.cpp71
1 files changed, 6 insertions, 65 deletions
diff --git a/src/key.cpp b/src/key.cpp
index 0ca9a681a3..76256b864c 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -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:
};
static CSecp256k1Init instance_of_csecp256k1;
-#endif
} // anon namespace
bool CKey::Check(const unsigned char *vch) {
@@ -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 {
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 {
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 {
} 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)
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
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]) {
}
bool ECC_InitSanityCheck() {
-#ifdef USE_SECP256K1
- return true;
-#else
return CECKey::SanityCheck();
-#endif
}