Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
bip324_ecdh.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present 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#include <key.h>
7#include <pubkey.h>
8#include <random.h>
9#include <span.h>
10
11#include <algorithm>
12#include <array>
13#include <cstddef>
14
16{
19
20 std::array<std::byte, 32> key_data;
21 std::array<std::byte, EllSwiftPubKey::size()> our_ellswift_data;
22 std::array<std::byte, EllSwiftPubKey::size()> their_ellswift_data;
23
24 rng.fillrand(key_data);
27
28 bench.batch(1).unit("ecdh").run([&] {
29 CKey key;
30 key.Set(key_data.data(), key_data.data() + 32, true);
33
35
36 // To make sure that the computation is not the same on every iteration (ellswift decoding
37 // is variable-time), distribute bytes from the shared secret over the 3 inputs. The most
38 // important one is their_ellswift, because that one is actually decoded, so it's given most
39 // bytes. The data is copied into the middle, so that both halves are affected:
40 // - Copy 8 bytes from the resulting shared secret into middle of the private key.
41 std::copy(ret.begin(), ret.begin() + 8, key_data.begin() + 12);
42 // - Copy 8 bytes from the resulting shared secret into the middle of our ellswift key.
43 std::copy(ret.begin() + 8, ret.begin() + 16, our_ellswift_data.begin() + 28);
44 // - Copy 16 bytes from the resulting shared secret into the middle of their ellswift key.
45 std::copy(ret.begin() + 16, ret.end(), their_ellswift_data.begin() + 24);
46 });
47}
48
#define BENCHMARK(n)
Definition bench.h:68
static void BIP324_ECDH(benchmark::Bench &bench)
int ret
ECC_Context ecc_context
An encapsulated private key.
Definition key.h:36
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
Definition key.cpp:328
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition key.h:104
RAII class initializing and deinitializing global state for elliptic curve support.
Definition key.h:326
Fast randomness source.
Definition random.h:386
void fillrand(std::span< std::byte > output) noexcept
Fill a byte span with random bytes.
Definition random.cpp:626
Main entry point to nanobench's benchmarking facility.
Definition nanobench.h:627
An ElligatorSwift-encoded public key.
Definition pubkey.h:309
static constexpr size_t size()
Definition pubkey.h:326
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73