Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
bench
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
#include <span>
15
16
static
void
BIP324_ECDH
(
benchmark::Bench
& bench)
17
{
18
ECC_Context
ecc_context
{};
19
FastRandomContext
rng
;
20
21
std::array<std::byte, 32> key_data;
22
std::array<std::byte,
EllSwiftPubKey::size
()> our_ellswift_data;
23
std::array<std::byte,
EllSwiftPubKey::size
()> their_ellswift_data;
24
25
rng
.
fillrand
(key_data);
26
rng
.
fillrand
(our_ellswift_data);
27
rng
.
fillrand
(their_ellswift_data);
28
29
bench.
batch
(1).
unit
(
"ecdh"
).
run
([&] {
30
CKey
key;
31
key.
Set
(key_data.data(), key_data.data() + 32,
true
);
32
EllSwiftPubKey
our_ellswift(our_ellswift_data);
33
EllSwiftPubKey
their_ellswift(their_ellswift_data);
34
35
auto
ret
= key.
ComputeBIP324ECDHSecret
(their_ellswift, our_ellswift,
true
);
36
37
// To make sure that the computation is not the same on every iteration (ellswift decoding
38
// is variable-time), distribute bytes from the shared secret over the 3 inputs. The most
39
// important one is their_ellswift, because that one is actually decoded, so it's given most
40
// bytes. The data is copied into the middle, so that both halves are affected:
41
// - Copy 8 bytes from the resulting shared secret into middle of the private key.
42
std::copy(
ret
.begin(),
ret
.begin() + 8, key_data.begin() + 12);
43
// - Copy 8 bytes from the resulting shared secret into the middle of our ellswift key.
44
std::copy(
ret
.begin() + 8,
ret
.begin() + 16, our_ellswift_data.begin() + 28);
45
// - Copy 16 bytes from the resulting shared secret into the middle of their ellswift key.
46
std::copy(
ret
.begin() + 16,
ret
.end(), their_ellswift_data.begin() + 24);
47
});
48
}
49
50
BENCHMARK
(
BIP324_ECDH
);
bench.h
BENCHMARK
#define BENCHMARK(n)
Definition
bench.h:69
BIP324_ECDH
static void BIP324_ECDH(benchmark::Bench &bench)
Definition
bip324_ecdh.cpp:16
ret
int ret
Definition
bitcoin-cli.cpp:1691
ecc_context
ECC_Context ecc_context
Definition
bitcoin-wallet.cpp:129
CKey
An encapsulated private key.
Definition
key.h:40
CKey::ComputeBIP324ECDHSecret
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
Definition
key.cpp:329
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition
key.h:108
ECC_Context
RAII class initializing and deinitializing global state for elliptic curve support.
Definition
key.h:338
FastRandomContext
Fast randomness source.
Definition
random.h:386
FastRandomContext::fillrand
void fillrand(std::span< std::byte > output) noexcept
Fill a byte span with random bytes.
Definition
random.cpp:626
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition
nanobench.h:649
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition
nanobench.h:1308
ankerl::nanobench::Bench::batch
Bench & batch(T b) noexcept
Sets the batch size.
Definition
nanobench.h:1332
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
key.h
pubkey.h
span.h
EllSwiftPubKey
An ElligatorSwift-encoded public key.
Definition
pubkey.h:315
EllSwiftPubKey::size
static constexpr size_t size()
Definition
pubkey.h:332
rng
FastRandomContext rng
Definition
dbwrapper.cpp:413
Generated on
for Bitcoin Core by
1.18.0