Skip to content

Commit 739c53b

Browse files
examples: Extend sig examples by call that uses static context
Besides improving the examples, this makes sure that the examples import a variable (instead of a function), namely the static context, from the library. This is helpful when testing MSVC builds, because the MSVC linker tends to be awkward when importing variables.
1 parent 914276e commit 739c53b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: examples/ecdsa.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(void) {
3434
unsigned char compressed_pubkey[33];
3535
unsigned char serialized_signature[64];
3636
size_t len;
37-
int is_signature_valid;
37+
int is_signature_valid, is_signature_valid2;
3838
int return_val;
3939
secp256k1_pubkey pubkey;
4040
secp256k1_ecdsa_signature sig;
@@ -116,10 +116,18 @@ int main(void) {
116116
printf("Signature: ");
117117
print_hex(serialized_signature, sizeof(serialized_signature));
118118

119-
120119
/* This will clear everything from the context and free the memory */
121120
secp256k1_context_destroy(ctx);
122121

122+
/* Bonus example: if all we need is signature verification (and no key
123+
generation or signing), we don't need to use a context created via
124+
secp256k1_context_create(). We can simply use the static (i.e., global)
125+
context secp256k1_context_static. See its description in
126+
include/secp256k1.h for details. */
127+
is_signature_valid2 = secp256k1_ecdsa_verify(secp256k1_context_static,
128+
&sig, msg_hash, &pubkey);
129+
assert(is_signature_valid2 == is_signature_valid);
130+
123131
/* It's best practice to try to clear secrets from memory after using them.
124132
* This is done because some bugs can allow an attacker to leak memory, for
125133
* example through "out of bounds" array access (see Heartbleed), Or the OS

Diff for: examples/schnorr.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(void) {
2626
unsigned char auxiliary_rand[32];
2727
unsigned char serialized_pubkey[32];
2828
unsigned char signature[64];
29-
int is_signature_valid;
29+
int is_signature_valid, is_signature_valid2;
3030
int return_val;
3131
secp256k1_xonly_pubkey pubkey;
3232
secp256k1_keypair keypair;
@@ -135,6 +135,15 @@ int main(void) {
135135
/* This will clear everything from the context and free the memory */
136136
secp256k1_context_destroy(ctx);
137137

138+
/* Bonus example: if all we need is signature verification (and no key
139+
generation or signing), we don't need to use a context created via
140+
secp256k1_context_create(). We can simply use the static (i.e., global)
141+
context secp256k1_context_static. See its description in
142+
include/secp256k1.h for details. */
143+
is_signature_valid2 = secp256k1_schnorrsig_verify(secp256k1_context_static,
144+
signature, msg_hash, 32, &pubkey);
145+
assert(is_signature_valid2 == is_signature_valid);
146+
138147
/* It's best practice to try to clear secrets from memory after using them.
139148
* This is done because some bugs can allow an attacker to leak memory, for
140149
* example through "out of bounds" array access (see Heartbleed), Or the OS

0 commit comments

Comments
 (0)