Skip to content

Commit 4340f89

Browse files
DEMO DEMO DEMO Make ECDH example use prealloc API to demo purpose of the previous commit
1 parent adde331 commit 4340f89

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

examples/ecdh.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <assert.h>
1212
#include <string.h>
1313

14-
#include <secp256k1.h>
14+
#include <secp256k1_preallocated.h>
1515
#include <secp256k1_ecdh.h>
1616

1717
#include "random.h"
@@ -30,8 +30,13 @@ int main(void) {
3030
secp256k1_pubkey pubkey1;
3131
secp256k1_pubkey pubkey2;
3232

33-
/* Before we can call actual API functions, we need to create a "context". */
34-
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_DEFAULT);
33+
/* The specification in secp256k1.h states that `secp256k1_ec_pubkey_create`
34+
* needs a context object initialized for signing, which is why we create
35+
* a context with the SECP256K1_CONTEXT_SIGN flag.
36+
* (The docs for `secp256k1_ecdh` don't require any special context, just
37+
* some initialized context) */
38+
unsigned char mem[1000];
39+
secp256k1_context* ctx = secp256k1_context_preallocated_create(mem, SECP256K1_CONTEXT_SIGN);
3540
if (!fill_random(randomize, sizeof(randomize))) {
3641
printf("Failed to generate randomness\n");
3742
return 1;
@@ -105,7 +110,7 @@ int main(void) {
105110
print_hex(shared_secret1, sizeof(shared_secret1));
106111

107112
/* This will clear everything from the context and free the memory */
108-
secp256k1_context_destroy(ctx);
113+
secp256k1_context_preallocated_destroy(ctx);
109114

110115
/* It's best practice to try to clear secrets from memory after using them.
111116
* This is done because some bugs can allow an attacker to leak memory, for

0 commit comments

Comments
 (0)