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/*************************************************************************2* Written in 2024 by Sebastian Falbesoner *3* To the extent possible under law, the author(s) have dedicated all *4* copyright and related and neighboring rights to the software in this *5* file to the public domain worldwide. This software is distributed *6* without any warranty. For the CC0 Public Domain Dedication, see *7* EXAMPLES_COPYING or https://creativecommons.org/publicdomain/zero/1.0 *8*************************************************************************/10/** This file demonstrates how to use the ElligatorSwift module to perform11* a key exchange according to BIP 324. Additionally, see the documentation12* in include/secp256k1_ellswift.h and doc/ellswift.md.13*/15#include <stdio.h>16#include <stdlib.h>17#include <assert.h>18#include <string.h>20#include <secp256k1.h>21#include <secp256k1_ellswift.h>23#include "examples_util.h"25int main(void) {26secp256k1_context* ctx;27unsigned char randomize[32];28unsigned char auxrand1[32];29unsigned char auxrand2[32];30unsigned char seckey1[32];31unsigned char seckey2[32];32unsigned char ellswift_pubkey1[64];33unsigned char ellswift_pubkey2[64];34unsigned char shared_secret1[32];35unsigned char shared_secret2[32];36int return_val;38/* Create a secp256k1 context */39ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);40if (!fill_random(randomize, sizeof(randomize))) {41printf("Failed to generate randomness\n");42return EXIT_FAILURE;43}44/* Randomizing the context is recommended to protect against side-channel45* leakage. See `secp256k1_context_randomize` in secp256k1.h for more46* information about it. This should never fail. */47return_val = secp256k1_context_randomize(ctx, randomize);48assert(return_val);50/*** Generate secret keys ***/51if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {52printf("Failed to generate randomness\n");53return EXIT_FAILURE;54}55/* If the secret key is zero or out of range (greater than secp256k1's56* order), we fail. Note that the probability of this occurring is negligible57* with a properly functioning random number generator. */58if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {59printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");60return EXIT_FAILURE;61}63/* Generate ElligatorSwift public keys. This should never fail with valid context and64verified secret keys. Note that providing additional randomness (fourth parameter) is65optional, but recommended. */66if (!fill_random(auxrand1, sizeof(auxrand1)) || !fill_random(auxrand2, sizeof(auxrand2))) {67printf("Failed to generate randomness\n");68return EXIT_FAILURE;69}70return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey1, seckey1, auxrand1);71assert(return_val);72return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey2, seckey2, auxrand2);73assert(return_val);75/*** Create the shared secret on each side ***/77/* Perform x-only ECDH with seckey1 and ellswift_pubkey2. Should never fail78* with a verified seckey and valid pubkey. Note that both parties pass both79* EllSwift pubkeys in the same order; the pubkey of the calling party is80* determined by the "party" boolean (sixth parameter). */81return_val = secp256k1_ellswift_xdh(ctx, shared_secret1, ellswift_pubkey1, ellswift_pubkey2,82seckey1, 0, secp256k1_ellswift_xdh_hash_function_bip324, NULL);83assert(return_val);85/* Perform x-only ECDH with seckey2 and ellswift_pubkey1. Should never fail86* with a verified seckey and valid pubkey. */87return_val = secp256k1_ellswift_xdh(ctx, shared_secret2, ellswift_pubkey1, ellswift_pubkey2,88seckey2, 1, secp256k1_ellswift_xdh_hash_function_bip324, NULL);89assert(return_val);91/* Both parties should end up with the same shared secret */92return_val = memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1));93assert(return_val == 0);95printf( " Secret Key1: ");96print_hex(seckey1, sizeof(seckey1));97printf( "EllSwift Pubkey1: ");98print_hex(ellswift_pubkey1, sizeof(ellswift_pubkey1));99printf("\n Secret Key2: ");100print_hex(seckey2, sizeof(seckey2));101printf( "EllSwift Pubkey2: ");102print_hex(ellswift_pubkey2, sizeof(ellswift_pubkey2));103printf("\n Shared Secret: ");104print_hex(shared_secret1, sizeof(shared_secret1));106/* This will clear everything from the context and free the memory */107secp256k1_context_destroy(ctx);109/* It's best practice to try to clear secrets from memory after using them.110* This is done because some bugs can allow an attacker to leak memory, for111* example through "out of bounds" array access (see Heartbleed), or the OS112* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.113*114* Here we are preventing these writes from being optimized out, as any good compiler115* will remove any writes that aren't used. */116secure_erase(seckey1, sizeof(seckey1));117secure_erase(seckey2, sizeof(seckey2));118secure_erase(shared_secret1, sizeof(shared_secret1));119secure_erase(shared_secret2, sizeof(shared_secret2));121return EXIT_SUCCESS;122}
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.