aboutsummaryrefslogtreecommitdiff
path: root/src/key.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-11-19 11:56:01 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2014-11-19 12:02:29 +0100
commit271061242b0bf2fd0864ffc053f0435eb1dcf992 (patch)
treef167f58faa8529804ae2068b33ff156e8e71b221 /src/key.cpp
parentbf217e3adc5d8c01bc9b01bac13f14c47332ddac (diff)
parentf9e40fb075aa639094213c47e135b2c363e2dc4c (diff)
Merge pull request #5220
f9e40fb revert part of 9eda1620b (Cory Fields) 4300876 build: secp256k1 as a subdir, since it's required (Cory Fields) 0dc8613 build: fixup configure to not export anything (Cory Fields) 54566de depends: quit exporting in config.site (Cory Fields) ff1e5ba depends: add gmp package (Cory Fields) cf61b54 Don't use bashisms in configure (Pieter Wuille) dffb8f8 Use libsecp256k1 in key.cpp (Pieter Wuille) 4c97c64 Do not use EC code in global constructors (Pieter Wuille) 07a9901 Always build and link libsecp256k1 (Pieter Wuille) d48555b Squashed 'src/secp256k1/' content from commit ad2028f (Pieter Wuille) 7a7e109 Delete src/secp256k1 before subtree import (Pieter Wuille)
Diffstat (limited to 'src/key.cpp')
-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
}