Skip to content

Commit 60672a6

Browse files
committed
Merge pull request #5256
f4e0aef Do signature-s negation inside the tests (Pieter Wuille)
2 parents 0c7862e + f4e0aef commit 60672a6

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

src/ecwrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool CECKey::SetPubKey(const unsigned char* pubkey, size_t size) {
193193
return o2i_ECPublicKey(&pkey, &pubkey, size) != NULL;
194194
}
195195

196-
bool CECKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) {
196+
bool CECKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) {
197197
vchSig.clear();
198198
ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
199199
if (sig == NULL)
@@ -205,7 +205,7 @@ bool CECKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool
205205
BIGNUM *halforder = BN_CTX_get(ctx);
206206
EC_GROUP_get_order(group, order, ctx);
207207
BN_rshift1(halforder, order);
208-
if (lowS && BN_cmp(sig->s, halforder) > 0) {
208+
if (BN_cmp(sig->s, halforder) > 0) {
209209
// enforce low S values, by negating the value (modulo the order) if above order/2.
210210
BN_sub(sig->s, order, sig->s);
211211
}

src/ecwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CECKey {
2828
bool SetPrivKey(const unsigned char* privkey, size_t size, bool fSkipCheck=false);
2929
void GetPubKey(std::vector<unsigned char>& pubkey, bool fCompressed);
3030
bool SetPubKey(const unsigned char* pubkey, size_t size);
31-
bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS);
31+
bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig);
3232
bool Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig);
3333
bool SignCompact(const uint256 &hash, unsigned char *p64, int &rec);
3434

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ CPubKey CKey::GetPubKey() const {
102102
return result;
103103
}
104104

105-
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lowS) const {
105+
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
106106
if (!fValid)
107107
return false;
108108
#ifdef USE_SECP256K1
@@ -119,7 +119,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool lo
119119
#else
120120
CECKey key;
121121
key.SetSecretBytes(vch);
122-
return key.Sign(hash, vchSig, lowS);
122+
return key.Sign(hash, vchSig);
123123
#endif
124124
}
125125

src/key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CKey
122122
CPubKey GetPubKey() const;
123123

124124
//! Create a DER-serialized signature.
125-
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool lowS = true) const;
125+
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig) const;
126126

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

src/test/script_tests.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,48 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, bo
9595
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, flags, SignatureChecker(BuildSpendingTransaction(scriptSig, BuildCreditingTransaction(scriptPubKey)), 0)) == expect, message);
9696
}
9797

98+
void static NegateSignatureS(std::vector<unsigned char>& vchSig) {
99+
// Parse the signature.
100+
std::vector<unsigned char> r, s;
101+
r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
102+
s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
103+
unsigned char hashtype = vchSig.back();
104+
105+
// Really ugly to implement mod-n negation here, but it would be feature creep to expose such functionality from libsecp256k1.
106+
static const unsigned char order[33] = {
107+
0x00,
108+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
109+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
110+
0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
111+
0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41
112+
};
113+
while (s.size() < 33) {
114+
s.insert(s.begin(), 0x00);
115+
}
116+
int carry = 0;
117+
for (int p = 32; p >= 1; p--) {
118+
int n = (int)order[p] - s[p] - carry;
119+
s[p] = (n + 256) & 0xFF;
120+
carry = (n < 0);
121+
}
122+
assert(carry == 0);
123+
if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) {
124+
s.erase(s.begin());
125+
}
126+
127+
// Reconstruct the signature.
128+
vchSig.clear();
129+
vchSig.push_back(0x30);
130+
vchSig.push_back(4 + r.size() + s.size());
131+
vchSig.push_back(0x02);
132+
vchSig.push_back(r.size());
133+
vchSig.insert(vchSig.end(), r.begin(), r.end());
134+
vchSig.push_back(0x02);
135+
vchSig.push_back(s.size());
136+
vchSig.insert(vchSig.end(), s.begin(), s.end());
137+
vchSig.push_back(hashtype);
138+
}
139+
98140
namespace
99141
{
100142
const unsigned char vchKey0[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
@@ -194,7 +236,10 @@ class TestBuilder
194236
uint256 hash = SignatureHash(scriptPubKey, spendTx, 0, nHashType);
195237
std::vector<unsigned char> vchSig, r, s;
196238
do {
197-
key.Sign(hash, vchSig, lenS <= 32);
239+
key.Sign(hash, vchSig);
240+
if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
241+
NegateSignatureS(vchSig);
242+
}
198243
r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
199244
s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
200245
} while (lenR != r.size() || lenS != s.size());

0 commit comments

Comments
 (0)