Skip to content

Commit a53fd41

Browse files
committed
Deterministic signing
1 parent 3060e36 commit a53fd41

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

src/key.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "key.h"
66

77
#include "crypto/hmac_sha512.h"
8+
#include "crypto/rfc6979_hmac_sha256.h"
89
#include "eccryptoverify.h"
910
#include "pubkey.h"
1011
#include "random.h"
@@ -71,30 +72,36 @@ CPubKey CKey::GetPubKey() const {
7172
return result;
7273
}
7374

74-
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
75+
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
7576
if (!fValid)
7677
return false;
7778
vchSig.resize(72);
78-
int nSigLen = 72;
79-
CKey nonce;
79+
RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
8080
do {
81-
nonce.MakeNewKey(true);
82-
if (secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), nonce.begin()))
83-
break;
81+
uint256 nonce;
82+
prng.Generate((unsigned char*)&nonce, 32);
83+
nonce += test_case;
84+
int nSigLen = 72;
85+
int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, 32, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
86+
vchSig.resize(nSigLen);
87+
nonce = 0;
88+
if (ret)
89+
return true;
8490
} while(true);
85-
vchSig.resize(nSigLen);
86-
return true;
8791
}
8892

8993
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
9094
if (!fValid)
9195
return false;
9296
vchSig.resize(65);
9397
int rec = -1;
94-
CKey nonce;
98+
RFC6979_HMAC_SHA256 prng(begin(), 32, (unsigned char*)&hash, 32);
9599
do {
96-
nonce.MakeNewKey(true);
97-
if (secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), nonce.begin(), &rec))
100+
uint256 nonce;
101+
prng.Generate((unsigned char*)&nonce, 32);
102+
int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, 32, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
103+
nonce = 0;
104+
if (ret)
98105
break;
99106
} while(true);
100107
assert(rec != -1);

src/key.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ class CKey
121121
*/
122122
CPubKey GetPubKey() const;
123123

124-
//! Create a DER-serialized signature.
125-
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig) const;
124+
/**
125+
* Create a DER-serialized signature.
126+
* The test_case parameter tweaks the deterministic nonce, and is only for
127+
* testing. It should be zero for normal use.
128+
*/
129+
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
126130

127131
/**
128132
* Create a compact signature (65 bytes), which allows reconstructing the used public key.

src/test/key_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "script/script.h"
99
#include "uint256.h"
1010
#include "util.h"
11+
#include "utilstrencodings.h"
1112

1213
#include <string>
1314
#include <vector>
@@ -142,6 +143,28 @@ BOOST_AUTO_TEST_CASE(key_test1)
142143
BOOST_CHECK(rkey1C == pubkey1C);
143144
BOOST_CHECK(rkey2C == pubkey2C);
144145
}
146+
147+
// test deterministic signing
148+
149+
std::vector<unsigned char> detsig, detsigc;
150+
string strMsg = "Very deterministic message";
151+
uint256 hashMsg = Hash(strMsg.begin(), strMsg.end());
152+
BOOST_CHECK(key1.Sign(hashMsg, detsig));
153+
BOOST_CHECK(key1C.Sign(hashMsg, detsigc));
154+
BOOST_CHECK(detsig == detsigc);
155+
BOOST_CHECK(detsig == ParseHex("304402205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d022014ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
156+
BOOST_CHECK(key2.Sign(hashMsg, detsig));
157+
BOOST_CHECK(key2C.Sign(hashMsg, detsigc));
158+
BOOST_CHECK(detsig == detsigc);
159+
BOOST_CHECK(detsig == ParseHex("3044022052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd5022061d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
160+
BOOST_CHECK(key1.SignCompact(hashMsg, detsig));
161+
BOOST_CHECK(key1C.SignCompact(hashMsg, detsigc));
162+
BOOST_CHECK(detsig == ParseHex("1c5dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
163+
BOOST_CHECK(detsigc == ParseHex("205dbbddda71772d95ce91cd2d14b592cfbc1dd0aabd6a394b6c2d377bbe59d31d14ddda21494a4e221f0824f0b8b924c43fa43c0ad57dccdaa11f81a6bd4582f6"));
164+
BOOST_CHECK(key2.SignCompact(hashMsg, detsig));
165+
BOOST_CHECK(key2C.SignCompact(hashMsg, detsigc));
166+
BOOST_CHECK(detsig == ParseHex("1c52d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
167+
BOOST_CHECK(detsigc == ParseHex("2052d8a32079c11e79db95af63bb9600c5b04f21a9ca33dc129c2bfa8ac9dc1cd561d8ae5e0f6c1a16bde3719c64c2fd70e404b6428ab9a69566962e8771b5944d"));
145168
}
146169

147170
BOOST_AUTO_TEST_SUITE_END()

src/test/script_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ class TestBuilder
248248
{
249249
uint256 hash = SignatureHash(scriptPubKey, spendTx, 0, nHashType);
250250
std::vector<unsigned char> vchSig, r, s;
251+
uint32_t iter = 0;
251252
do {
252-
key.Sign(hash, vchSig);
253+
key.Sign(hash, vchSig, iter++);
253254
if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
254255
NegateSignatureS(vchSig);
255256
}

0 commit comments

Comments
 (0)