Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <bench/bench.h>6#include <key.h>7#include <pubkey.h>8#include <random.h>9#include <span.h>11#include <algorithm>12#include <array>13#include <cstddef>14#include <span>16static void BIP324_ECDH(benchmark::Bench& bench)17{18ECC_Context ecc_context{};19FastRandomContext rng;21std::array<std::byte, 32> key_data;22std::array<std::byte, EllSwiftPubKey::size()> our_ellswift_data;23std::array<std::byte, EllSwiftPubKey::size()> their_ellswift_data;25rng.fillrand(key_data);26rng.fillrand(our_ellswift_data);27rng.fillrand(their_ellswift_data);29bench.batch(1).unit("ecdh").run([&] {30CKey key;31key.Set(key_data.data(), key_data.data() + 32, true);32EllSwiftPubKey our_ellswift(our_ellswift_data);33EllSwiftPubKey their_ellswift(their_ellswift_data);35auto ret = key.ComputeBIP324ECDHSecret(their_ellswift, our_ellswift, true);37// To make sure that the computation is not the same on every iteration (ellswift decoding38// is variable-time), distribute bytes from the shared secret over the 3 inputs. The most39// important one is their_ellswift, because that one is actually decoded, so it's given most40// 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.42std::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.44std::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.46std::copy(ret.begin() + 16, ret.end(), their_ellswift_data.begin() + 24);47});48}50BENCHMARK(BIP324_ECDH);
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.