|
| 1 | +// Copyright (c) 2022 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <bench/bench.h> |
| 6 | + |
| 7 | +#include <key.h> |
| 8 | +#include <pubkey.h> |
| 9 | +#include <random.h> |
| 10 | +#include <secp256k1.h> |
| 11 | +#include <secp256k1_ellswift.h> |
| 12 | + |
| 13 | +#include <cstddef> |
| 14 | + |
| 15 | +CKey GetRandomKey() |
| 16 | +{ |
| 17 | + CKey key; |
| 18 | + key.MakeNewKey(true); |
| 19 | + return key; |
| 20 | +} |
| 21 | + |
| 22 | +int GetEll64(const CKey& key, unsigned char* ell64, secp256k1_context* ctx) |
| 23 | +{ |
| 24 | + std::array<unsigned char, 32> rnd32; |
| 25 | + GetRandBytes(rnd32); |
| 26 | + return secp256k1_ellswift_create(ctx, ell64, reinterpret_cast<const unsigned char*>(key.data()), rnd32.data()); |
| 27 | +} |
| 28 | + |
| 29 | +static void BIP324_ECDH(benchmark::Bench& bench) |
| 30 | +{ |
| 31 | + ECC_Start(); |
| 32 | + secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE); |
| 33 | + assert(ctx != nullptr); |
| 34 | + assert(secp256k1_context_randomize(ctx, nullptr)); |
| 35 | + auto our_key = GetRandomKey(); |
| 36 | + auto their_key = GetRandomKey(); |
| 37 | + |
| 38 | + unsigned char our_ell64[64], their_ell64[64]; |
| 39 | + if (!GetEll64(our_key, our_ell64, ctx)) { |
| 40 | + assert(false); |
| 41 | + } |
| 42 | + |
| 43 | + if (!GetEll64(their_key, their_ell64, ctx)) { |
| 44 | + assert(false); |
| 45 | + } |
| 46 | + |
| 47 | + bench.batch(1).unit("ecdh").run([&] { |
| 48 | + assert(our_key.ComputeBIP324ECDHSecret({reinterpret_cast<std::byte*>(their_ell64), 64}, |
| 49 | + {reinterpret_cast<std::byte*>(our_ell64), 64}, |
| 50 | + true) |
| 51 | + .has_value()); |
| 52 | + }); |
| 53 | + secp256k1_context_destroy(ctx); |
| 54 | + ECC_Stop(); |
| 55 | +} |
| 56 | + |
| 57 | +BENCHMARK(BIP324_ECDH, benchmark::PriorityLevel::HIGH); |
0 commit comments