commit 4a7ed18efff0cc98f56f876f5d7e63b587816bd7
parent 41aa0587f3ca62fa7ccf915adc0eaca697818e7d
Author: Shinoa-Fores <btcinfo@sdf.org>
Date: Wed, 13 Jan 2021 17:33:06 -0500
mod6_der_high_low_s.vpatch
Diffstat:
4 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/bitcoin/src/init.cpp b/bitcoin/src/init.cpp
@@ -177,6 +177,8 @@ bool AppInit2(int argc, char* argv[])
" -verifyall \t\t " + _("Forbid the skipping of ECDSA signature verification between checkpoints.\n") +
" -setverstring \t\t " + _("Set a custom version string.\n") +
" -setvernum \t\t " + _("Set a custom version number.\n") +
+ " -highs \t\t " + _("Set all transactions to have DER 'S' Value set to 'high'.\n") +
+ " -lows \t\t " + _("Set all transactions to have DER 'S' Value set to 'low'.\n") +
" -logtimestamps \t " + _("Prepend debug output with timestamp\n") +
" -printtoconsole \t " + _("Send trace/debug info to console instead of debug.log file\n") +
" -rpcuser=<user> \t " + _("Username for JSON-RPC connections\n") +
@@ -200,6 +202,14 @@ bool AppInit2(int argc, char* argv[])
fDaemon = GetBoolArg("-daemon");
fCanEat = GetBoolArg("-caneat");
fVerifyAll = GetBoolArg("-verifyall");
+ fHighS = GetBoolArg("-highs");
+ fLowS = GetBoolArg("-lows");
+
+ if (fHighS && fLowS)
+ {
+ printf("Error: '-highs' and '-lows' can not be set at the same time.\n");
+ return false;
+ }
if (mapArgs.count("-setverstring"))
{
diff --git a/bitcoin/src/key.h b/bitcoin/src/key.h
@@ -291,12 +291,46 @@ public:
bool Sign(uint256 hash, std::vector<unsigned char>& vchSig)
{
vchSig.clear();
- unsigned char pchSig[10000];
- unsigned int nSize = 0;
- if (!ECDSA_sign(0, (unsigned char*)&hash, sizeof(hash), pchSig, &nSize, pkey))
+ ECDSA_SIG *sig = ECDSA_do_sign((unsigned char *) &hash, sizeof(hash), pkey);
+
+ if (sig == NULL)
+ {
+ printf("ERROR, ECDSA_sign failed in key.h:Sign()\n");
return false;
- vchSig.resize(nSize);
- memcpy(&vchSig[0], pchSig, nSize);
+ }
+
+ BN_CTX *ctx = BN_CTX_new();
+ BN_CTX_start(ctx);
+ const EC_GROUP *group = EC_KEY_get0_group(pkey);
+ BIGNUM *order = BN_CTX_get(ctx);
+ BIGNUM *halforder = BN_CTX_get(ctx);
+ EC_GROUP_get_order(group, order, ctx);
+ BN_rshift1(halforder, order);
+
+ if (fHighS && (BN_cmp(sig->s, halforder) < 0))
+ {
+ // enforce high S values
+ BN_sub(sig->s, order, sig->s);
+ }
+
+ if (fLowS && (BN_cmp(sig->s, halforder) > 0))
+ {
+ // enforce low S values
+ BN_sub(sig->s, order, sig->s);
+ }
+
+ BN_CTX_end(ctx);
+ BN_CTX_free(ctx);
+ unsigned int nSize = ECDSA_size(pkey);
+ vchSig.resize(nSize); // Make sure it is big enough
+ unsigned char *pos = &vchSig[0];
+ nSize = i2d_ECDSA_SIG(sig, &pos);
+ //printf("DEBUG DER R: 0x%s\n", BN_bn2hex(sig->r));
+ //printf("DEBUG DER R: %s\n", BN_bn2dec(sig->r));
+ //printf("DEBUG DER S: 0x%s\n", BN_bn2hex(sig->s));
+ //printf("DEBUG DER S: %s\n", BN_bn2dec(sig->s));
+ ECDSA_SIG_free(sig);
+ vchSig.resize(nSize); // Shrink to fit actual size
return true;
}
diff --git a/bitcoin/src/util.cpp b/bitcoin/src/util.cpp
@@ -32,6 +32,8 @@ bool fCommandLine = false;
string strMiscWarning;
bool fNoListen = false;
bool fLogTimestamps = false;
+bool fLowS = false;
+bool fHighS = false;
std::string CLIENT_NAME(DEFAULT_CLIENT_NAME);
diff --git a/bitcoin/src/util.h b/bitcoin/src/util.h
@@ -122,6 +122,8 @@ extern std::string strMiscWarning;
extern bool fNoListen;
extern bool fLogTimestamps;
extern std::string CLIENT_NAME;
+extern bool fLowS;
+extern bool fHighS;
void RandAddSeed();
void RandAddSeedPerfmon();