Bitcoin Core  28.1.0
P2P Digital Currency
ellswift.c
Go to the documentation of this file.
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  *************************************************************************/
9 
15 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 
19 #include <secp256k1.h>
20 #include <secp256k1_ellswift.h>
21 
22 #include "examples_util.h"
23 
24 int main(void) {
25  secp256k1_context* ctx;
26  unsigned char randomize[32];
27  unsigned char auxrand1[32];
28  unsigned char auxrand2[32];
29  unsigned char seckey1[32];
30  unsigned char seckey2[32];
31  unsigned char ellswift_pubkey1[64];
32  unsigned char ellswift_pubkey2[64];
33  unsigned char shared_secret1[32];
34  unsigned char shared_secret2[32];
35  int return_val;
36 
37  /* Create a secp256k1 context */
39  if (!fill_random(randomize, sizeof(randomize))) {
40  printf("Failed to generate randomness\n");
41  return 1;
42  }
43  /* Randomizing the context is recommended to protect against side-channel
44  * leakage. See `secp256k1_context_randomize` in secp256k1.h for more
45  * information about it. This should never fail. */
46  return_val = secp256k1_context_randomize(ctx, randomize);
47  assert(return_val);
48 
49  /*** Generate secret keys ***/
50 
51  /* If the secret key is zero or out of range (bigger than secp256k1's
52  * order), we try to sample a new key. Note that the probability of this
53  * happening is negligible. */
54  while (1) {
55  if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
56  printf("Failed to generate randomness\n");
57  return 1;
58  }
59  if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
60  break;
61  }
62  }
63 
64  /* Generate ElligatorSwift public keys. This should never fail with valid context and
65  verified secret keys. Note that providing additional randomness (fourth parameter) is
66  optional, but recommended. */
67  if (!fill_random(auxrand1, sizeof(auxrand1)) || !fill_random(auxrand2, sizeof(auxrand2))) {
68  printf("Failed to generate randomness\n");
69  return 1;
70  }
71  return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey1, seckey1, auxrand1);
72  assert(return_val);
73  return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey2, seckey2, auxrand2);
74  assert(return_val);
75 
76  /*** Create the shared secret on each side ***/
77 
78  /* Perform x-only ECDH with seckey1 and ellswift_pubkey2. Should never fail
79  * with a verified seckey and valid pubkey. Note that both parties pass both
80  * EllSwift pubkeys in the same order; the pubkey of the calling party is
81  * determined by the "party" boolean (sixth parameter). */
82  return_val = secp256k1_ellswift_xdh(ctx, shared_secret1, ellswift_pubkey1, ellswift_pubkey2,
84  assert(return_val);
85 
86  /* Perform x-only ECDH with seckey2 and ellswift_pubkey1. Should never fail
87  * with a verified seckey and valid pubkey. */
88  return_val = secp256k1_ellswift_xdh(ctx, shared_secret2, ellswift_pubkey1, ellswift_pubkey2,
90  assert(return_val);
91 
92  /* Both parties should end up with the same shared secret */
93  return_val = memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1));
94  assert(return_val == 0);
95 
96  printf( " Secret Key1: ");
97  print_hex(seckey1, sizeof(seckey1));
98  printf( "EllSwift Pubkey1: ");
99  print_hex(ellswift_pubkey1, sizeof(ellswift_pubkey1));
100  printf("\n Secret Key2: ");
101  print_hex(seckey2, sizeof(seckey2));
102  printf( "EllSwift Pubkey2: ");
103  print_hex(ellswift_pubkey2, sizeof(ellswift_pubkey2));
104  printf("\n Shared Secret: ");
105  print_hex(shared_secret1, sizeof(shared_secret1));
106 
107  /* This will clear everything from the context and free the memory */
109 
110  /* It's best practice to try to clear secrets from memory after using them.
111  * This is done because some bugs can allow an attacker to leak memory, for
112  * example through "out of bounds" array access (see Heartbleed), or the OS
113  * swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
114  *
115  * Here we are preventing these writes from being optimized out, as any good compiler
116  * will remove any writes that aren't used. */
117  secure_erase(seckey1, sizeof(seckey1));
118  secure_erase(seckey2, sizeof(seckey2));
119  secure_erase(shared_secret1, sizeof(shared_secret1));
120  secure_erase(shared_secret2, sizeof(shared_secret2));
121 
122  return 0;
123 }
assert(!tx.IsCoinBase())
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:768
static void secure_erase(void *ptr, size_t len)
Definition: examples_util.h:86
SECP256K1_API const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324
An implementation of an secp256k1_ellswift_xdh_hash_function compatible with BIP324.
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context_preallocated_create.
Definition: secp256k1.h:205
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:187
static void print_hex(unsigned char *data, size_t size)
Definition: examples_util.h:72
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
Definition: secp256k1.c:590
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7)
Given a private key, and ElligatorSwift public keys sent in both directions, compute a shared secret ...
Definition: main_impl.h:549
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute an ElligatorSwift public key for a secret key.
Definition: main_impl.h:450
static int fill_random(unsigned char *data, size_t size)
Definition: examples_util.h:43
int main(void)
This file demonstrates how to use the ElligatorSwift module to perform a key exchange according to BI...
Definition: ellswift.c:24
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1076
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:141