Skip to content

Commit 38a07df

Browse files
committed
testing..
1 parent 7d07c29 commit 38a07df

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

src/bench/bench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace benchmark {
7373
std::map<std::string, uint8_t> map_label_priority = {
7474
{"high", PriorityLevel::HIGH},
7575
{"low", PriorityLevel::LOW},
76+
{"special", PriorityLevel::SPECIAL},
7677
{"all", 0xff}
7778
};
7879

src/bench/bench.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum PriorityLevel : uint8_t
4646
{
4747
LOW = 1 << 0,
4848
HIGH = 1 << 2,
49+
SPECIAL = 1 << 4,
4950
};
5051

5152
// List priority labels, comma-separated and sorted by increasing priority

src/bench/verify_script.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// modified to measure performance of other types of scripts.
2323
static void VerifyScriptBench(benchmark::Bench& bench)
2424
{
25-
ECC_Context ecc_context{};
25+
ECC_Context ecc_context{/*use_new_sign=*/false};
2626

2727
const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
2828
const int witnessversion = 0;
@@ -66,6 +66,55 @@ static void VerifyScriptBench(benchmark::Bench& bench)
6666
});
6767
}
6868

69+
// Microbenchmark for verification of a basic P2WPKH script. Can be easily
70+
// modified to measure performance of other types of scripts.
71+
static void VerifyScriptBenchNew(benchmark::Bench& bench)
72+
{
73+
SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI);
74+
ECC_Context ecc_context{/*use_new_sign=*/true};
75+
76+
const script_verify_flags flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
77+
const int witnessversion = 0;
78+
79+
// Key pair.
80+
CKey key;
81+
static const std::array<unsigned char, 32> vchKey = {
82+
{
83+
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
84+
}
85+
};
86+
key.Set(vchKey.begin(), vchKey.end(), false);
87+
CPubKey pubkey = key.GetPubKey();
88+
uint160 pubkeyHash;
89+
CHash160().Write(pubkey).Finalize(pubkeyHash);
90+
91+
// Script.
92+
CScript scriptPubKey = CScript() << witnessversion << ToByteVector(pubkeyHash);
93+
CScript scriptSig;
94+
CScript witScriptPubkey = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkeyHash) << OP_EQUALVERIFY << OP_CHECKSIG;
95+
const CMutableTransaction& txCredit = BuildCreditingTransaction(scriptPubKey, 1);
96+
CMutableTransaction txSpend = BuildSpendingTransaction(scriptSig, CScriptWitness(), CTransaction(txCredit));
97+
CScriptWitness& witness = txSpend.vin[0].scriptWitness;
98+
witness.stack.emplace_back();
99+
key.Sign(SignatureHash(witScriptPubkey, txSpend, 0, SIGHASH_ALL, txCredit.vout[0].nValue, SigVersion::WITNESS_V0), witness.stack.back());
100+
witness.stack.back().push_back(static_cast<unsigned char>(SIGHASH_ALL));
101+
witness.stack.push_back(ToByteVector(pubkey));
102+
103+
// Benchmark.
104+
bench.run([&] {
105+
ScriptError err;
106+
bool success = VerifyScript(
107+
txSpend.vin[0].scriptSig,
108+
txCredit.vout[0].scriptPubKey,
109+
&txSpend.vin[0].scriptWitness,
110+
flags,
111+
MutableTransactionSignatureChecker(&txSpend, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL),
112+
&err);
113+
assert(err == SCRIPT_ERR_OK);
114+
assert(success);
115+
});
116+
}
117+
69118
static void VerifyNestedIfScript(benchmark::Bench& bench)
70119
{
71120
std::vector<std::vector<unsigned char>> stack;
@@ -87,5 +136,6 @@ static void VerifyNestedIfScript(benchmark::Bench& bench)
87136
});
88137
}
89138

90-
BENCHMARK(VerifyScriptBench, benchmark::PriorityLevel::HIGH);
91-
BENCHMARK(VerifyNestedIfScript, benchmark::PriorityLevel::HIGH);
139+
BENCHMARK(VerifyScriptBench, benchmark::PriorityLevel::SPECIAL);
140+
BENCHMARK(VerifyScriptBenchNew, benchmark::PriorityLevel::SPECIAL);
141+
// BENCHMARK(VerifyNestedIfScript, benchmark::PriorityLevel::HIGH);

src/crypto/sha256.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ CSHA256::CSHA256()
698698
CSHA256& CSHA256::Write(const unsigned char* data, size_t len)
699699
{
700700
const unsigned char* end = data + len;
701-
size_t bufsize = bytes % 64;
701+
size_t bufsize = bytes & 0x3F;
702702
if (bufsize && bufsize + len >= 64) {
703703
// Fill the buffer, and process it.
704704
memcpy(buf + bufsize, data, 64 - bufsize);

src/key.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ bool ECC_InitSanityCheck() {
569569
}
570570

571571
/** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */
572-
static void ECC_Start() {
572+
static void ECC_Start(bool use_new_sign = false) {
573573
assert(secp256k1_context_sign == nullptr);
574574

575575
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
@@ -583,7 +583,7 @@ static void ECC_Start() {
583583
assert(ret);
584584
}
585585

586-
secp256k1_set_sha256_transform_callback(GetSha256TransformFn());
586+
if (use_new_sign) secp256k1_set_sha256_transform_callback(GetSha256TransformFn());
587587
secp256k1_context_sign = ctx;
588588
}
589589

@@ -597,9 +597,9 @@ static void ECC_Stop() {
597597
}
598598
}
599599

600-
ECC_Context::ECC_Context()
600+
ECC_Context::ECC_Context(bool use_new_sign)
601601
{
602-
ECC_Start();
602+
ECC_Start(use_new_sign);
603603
}
604604

605605
ECC_Context::~ECC_Context()

src/key.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ bool ECC_InitSanityCheck();
325325
class ECC_Context
326326
{
327327
public:
328-
ECC_Context();
328+
ECC_Context(bool use_new_sign = false);
329329
~ECC_Context();
330330
};
331331

0 commit comments

Comments
 (0)