2023-01-12 22:26:21 +01:00
|
|
|
// Copyright (c) 2018-2022 The Dash Core developers
|
2018-09-14 18:04:45 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <bench/bench.h>
|
2018-09-14 18:04:45 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <key.h>
|
2018-09-14 18:04:45 +02:00
|
|
|
|
2021-06-26 12:03:16 +02:00
|
|
|
static void ECDSASign(benchmark::Bench& bench)
|
2018-09-14 18:04:45 +02:00
|
|
|
{
|
|
|
|
std::vector<CKey> keys;
|
|
|
|
std::vector<uint256> hashes;
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
CKey k;
|
|
|
|
k.MakeNewKey(false);
|
|
|
|
keys.emplace_back(k);
|
|
|
|
hashes.emplace_back(::SerializeHash((int)i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark.
|
|
|
|
size_t i = 0;
|
2021-06-26 12:03:16 +02:00
|
|
|
bench.run([&] {
|
2018-09-14 18:04:45 +02:00
|
|
|
std::vector<unsigned char> sig;
|
|
|
|
keys[i].Sign(hashes[i], sig);
|
|
|
|
i = (i + 1) % keys.size();
|
2021-06-26 12:03:16 +02:00
|
|
|
});
|
2018-09-14 18:04:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 12:03:16 +02:00
|
|
|
static void ECDSAVerify(benchmark::Bench& bench)
|
2018-09-14 18:04:45 +02:00
|
|
|
{
|
|
|
|
std::vector<CPubKey> keys;
|
|
|
|
std::vector<uint256> hashes;
|
|
|
|
std::vector<std::vector<unsigned char>> sigs;
|
|
|
|
for (size_t i = 0; i < 100; i++) {
|
|
|
|
CKey k;
|
|
|
|
k.MakeNewKey(false);
|
|
|
|
keys.emplace_back(k.GetPubKey());
|
|
|
|
hashes.emplace_back(::SerializeHash((int)i));
|
|
|
|
std::vector<unsigned char> sig;
|
|
|
|
k.Sign(hashes[i], sig);
|
|
|
|
sigs.emplace_back(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark.
|
|
|
|
size_t i = 0;
|
2021-06-26 12:03:16 +02:00
|
|
|
bench.run([&] {
|
2018-09-14 18:04:45 +02:00
|
|
|
keys[i].Verify(hashes[i], sigs[i]);
|
|
|
|
i = (i + 1) % keys.size();
|
2021-06-26 12:03:16 +02:00
|
|
|
});
|
2018-09-14 18:04:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 12:03:16 +02:00
|
|
|
static void ECDSAVerify_LargeBlock(benchmark::Bench& bench)
|
2018-09-14 18:04:45 +02:00
|
|
|
{
|
|
|
|
std::vector<CPubKey> keys;
|
|
|
|
std::vector<uint256> hashes;
|
|
|
|
std::vector<std::vector<unsigned char>> sigs;
|
|
|
|
for (size_t i = 0; i < 1000; i++) {
|
|
|
|
CKey k;
|
|
|
|
k.MakeNewKey(false);
|
|
|
|
keys.emplace_back(k.GetPubKey());
|
|
|
|
hashes.emplace_back(::SerializeHash((int)i));
|
|
|
|
std::vector<unsigned char> sig;
|
|
|
|
k.Sign(hashes[i], sig);
|
|
|
|
sigs.emplace_back(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark.
|
2021-06-26 12:03:16 +02:00
|
|
|
bench.run([&] {
|
2018-09-14 18:04:45 +02:00
|
|
|
for (size_t i = 0; i < keys.size(); i++) {
|
|
|
|
keys[i].Verify(hashes[i], sigs[i]);
|
|
|
|
}
|
2021-06-26 12:03:16 +02:00
|
|
|
});
|
2018-09-14 18:04:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 12:03:16 +02:00
|
|
|
BENCHMARK(ECDSASign)
|
|
|
|
BENCHMARK(ECDSAVerify)
|
|
|
|
BENCHMARK(ECDSAVerify_LargeBlock)
|