aboutsummaryrefslogtreecommitdiff
path: root/src/key.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2014-12-18 14:49:19 +0100
committerPieter Wuille <pieter.wuille@gmail.com>2015-01-06 00:28:44 +0100
commit1a9576de9dbb1910cb8462e513938d45ef7b5a23 (patch)
treead075be51862d5612f3f2957a9bf3ce576b699e7 /src/key.cpp
parentec20fd74b89272a4f53337ccdf7683a7f52bab11 (diff)
Use libsecp256k1's RFC6979 implementation
Diffstat (limited to 'src/key.cpp')
-rw-r--r--src/key.cpp45
1 files changed, 21 insertions, 24 deletions
diff --git a/src/key.cpp b/src/key.cpp
index 2235c271d1..d8319db1a3 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -6,7 +6,6 @@
#include "arith_uint256.h"
#include "crypto/hmac_sha512.h"
-#include "crypto/rfc6979_hmac_sha256.h"
#include "eccryptoverify.h"
#include "pubkey.h"
#include "random.h"
@@ -74,23 +73,28 @@ CPubKey CKey::GetPubKey() const {
return result;
}
+extern "C"
+{
+static int secp256k1_nonce_function_test_case(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int attempt, const void *data)
+{
+ const uint32_t *test_case = static_cast<const uint32_t*>(data);
+ uint256 nonce;
+ secp256k1_nonce_function_rfc6979(nonce.begin(), msg32, key32, attempt, NULL);
+ nonce = ArithToUint256(UintToArith256(nonce) + *test_case);
+ memcpy(nonce32, nonce.begin(), 32);
+ return 1;
+}
+}
+
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
if (!fValid)
return false;
vchSig.resize(72);
- RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
- do {
- uint256 nonce;
- prng.Generate((unsigned char*)&nonce, 32);
- nonce = ArithToUint256(UintToArith256(nonce) + test_case);
- int nSigLen = 72;
- int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
- nonce = uint256();
- if (ret) {
- vchSig.resize(nSigLen);
- return true;
- }
- } while(true);
+ int nSigLen = 72;
+ int ret = secp256k1_ecdsa_sign(hash.begin(), (unsigned char*)&vchSig[0], &nSigLen, begin(), test_case == 0 ? secp256k1_nonce_function_rfc6979 : secp256k1_nonce_function_test_case, test_case == 0 ? NULL : &test_case);
+ assert(ret);
+ vchSig.resize(nSigLen);
+ return true;
}
bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
@@ -101,7 +105,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
std::string str = "Bitcoin key verification\n";
GetRandBytes(rnd, sizeof(rnd));
uint256 hash;
- CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize((unsigned char*)&hash);
+ CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
std::vector<unsigned char> vchSig;
Sign(hash, vchSig);
return pubkey.Verify(hash, vchSig);
@@ -112,15 +116,8 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
return false;
vchSig.resize(65);
int rec = -1;
- RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
- do {
- uint256 nonce;
- prng.Generate((unsigned char*)&nonce, 32);
- int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
- nonce = uint256();
- if (ret)
- break;
- } while(true);
+ int ret = secp256k1_ecdsa_sign_compact(hash.begin(), &vchSig[1], begin(), secp256k1_nonce_function_rfc6979, NULL, &rec);
+ assert(ret);
assert(rec != -1);
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
return true;