Skip to content

Commit 3168b08

Browse files
sipadhruv
andcommitted
Bench test for EllSwift ECDH
Co-authored-by: Dhruv Mehta <[email protected]>
1 parent 42d759f commit 3168b08

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/Makefile.bench.include

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ bench_bench_bitcoin_SOURCES = \
1818
bench/bench.cpp \
1919
bench/bench.h \
2020
bench/bench_bitcoin.cpp \
21+
bench/bip324_ecdh.cpp \
2122
bench/block_assemble.cpp \
2223
bench/ccoins_caching.cpp \
2324
bench/chacha20.cpp \

src/bench/bip324_ecdh.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 <span.h>
11+
12+
#include <array>
13+
#include <cstddef>
14+
15+
static void BIP324_ECDH(benchmark::Bench& bench)
16+
{
17+
ECC_Start();
18+
FastRandomContext rng;
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);
25+
rng.fillrand(our_ellswift_data);
26+
rng.fillrand(their_ellswift_data);
27+
28+
bench.batch(1).unit("ecdh").run([&] {
29+
CKey key;
30+
key.Set(UCharCast(key_data.data()), UCharCast(key_data.data()) + 32, true);
31+
EllSwiftPubKey our_ellswift(our_ellswift_data);
32+
EllSwiftPubKey their_ellswift(their_ellswift_data);
33+
34+
auto ret = key.ComputeBIP324ECDHSecret(their_ellswift, our_ellswift, true);
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+
ECC_Stop();
49+
}
50+
51+
BENCHMARK(BIP324_ECDH, benchmark::PriorityLevel::HIGH);

src/random.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,12 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
599599
return ret;
600600
}
601601

602+
void FastRandomContext::fillrand(Span<std::byte> output)
603+
{
604+
if (requires_seed) RandomSeed();
605+
rng.Keystream(UCharCast(output.data()), output.size());
606+
}
607+
602608
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bitbuf_size(0)
603609
{
604610
rng.SetKey32(seed.begin());

src/random.h

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ class FastRandomContext
213213
/** Generate random bytes. */
214214
std::vector<unsigned char> randbytes(size_t len);
215215

216+
/** Fill a byte Span with random bytes. */
217+
void fillrand(Span<std::byte> output);
218+
216219
/** Generate a random 32-bit integer. */
217220
uint32_t rand32() noexcept { return randbits(32); }
218221

0 commit comments

Comments
 (0)