11
11
#include <assert.h>
12
12
#include <string.h>
13
13
14
- #include <secp256k1 .h>
14
+ #include <secp256k1_preallocated .h>
15
15
#include <secp256k1_ecdh.h>
16
16
17
17
#include "random.h"
@@ -30,8 +30,13 @@ int main(void) {
30
30
secp256k1_pubkey pubkey1 ;
31
31
secp256k1_pubkey pubkey2 ;
32
32
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 );
35
40
if (!fill_random (randomize , sizeof (randomize ))) {
36
41
printf ("Failed to generate randomness\n" );
37
42
return 1 ;
@@ -105,7 +110,7 @@ int main(void) {
105
110
print_hex (shared_secret1 , sizeof (shared_secret1 ));
106
111
107
112
/* This will clear everything from the context and free the memory */
108
- secp256k1_context_destroy (ctx );
113
+ secp256k1_context_preallocated_destroy (ctx );
109
114
110
115
/* It's best practice to try to clear secrets from memory after using them.
111
116
* This is done because some bugs can allow an attacker to leak memory, for
0 commit comments