Skip to content

Commit b70d3e6

Browse files
committed
bitcoin-core#1609-swap magic numbers for exit codes in examples
1 parent 01b5893 commit b70d3e6

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

examples/ecdh.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <assert.h>
1212
#include <string.h>
13+
#include <stdlib.h>
1314

1415
#include <secp256k1.h>
1516
#include <secp256k1_ecdh.h>
@@ -33,7 +34,7 @@ int main(void) {
3334
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
3435
if (!fill_random(randomize, sizeof(randomize))) {
3536
printf("Failed to generate randomness\n");
36-
return 1;
37+
return EXIT_FAILURE;
3738
}
3839
/* Randomizing the context is recommended to protect against side-channel
3940
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
@@ -44,14 +45,14 @@ int main(void) {
4445
/*** Key Generation ***/
4546
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
4647
printf("Failed to generate randomness\n");
47-
return 1;
48+
return EXIT_FAILURE;
4849
}
4950
/* If the secret key is zero or out of range (greater than secp256k1's
5051
* order), we fail. Note that the probability of this occurring
5152
* is negligible with a properly functioning random number generator. */
5253
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
5354
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
54-
return 1;
55+
return EXIT_FAILURE;
5556
}
5657

5758
/* Public key creation using a valid context with a verified secret key should never fail */
@@ -116,5 +117,5 @@ int main(void) {
116117
secure_erase(shared_secret1, sizeof(shared_secret1));
117118
secure_erase(shared_secret2, sizeof(shared_secret2));
118119

119-
return 0;
120+
return EXIT_SUCCESS;
120121
}

examples/ecdsa.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <assert.h>
1212
#include <string.h>
13+
#include <stdlib.h>
1314

1415
#include <secp256k1.h>
1516

@@ -40,7 +41,7 @@ int main(void) {
4041
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
4142
if (!fill_random(randomize, sizeof(randomize))) {
4243
printf("Failed to generate randomness\n");
43-
return 1;
44+
return EXIT_FAILURE;
4445
}
4546
/* Randomizing the context is recommended to protect against side-channel
4647
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
@@ -50,15 +51,15 @@ int main(void) {
5051

5152
/*** Key Generation ***/
5253
/* If the secret key is zero or out of range (greater than secp256k1's
53-
* order), we return 1. Note that the probability of this occurring
54+
* order), we fail. Note that the probability of this occurring
5455
* is negligible with a properly functioning random number generator. */
5556
if (!fill_random(seckey, sizeof(seckey))) {
5657
printf("Failed to generate randomness\n");
57-
return 1;
58+
return EXIT_FAILURE;
5859
}
5960
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
6061
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
61-
return 1;
62+
return EXIT_FAILURE;
6263
}
6364

6465
/* Public key creation using a valid context with a verified secret key should never fail */
@@ -92,13 +93,13 @@ int main(void) {
9293
/* Deserialize the signature. This will return 0 if the signature can't be parsed correctly. */
9394
if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, serialized_signature)) {
9495
printf("Failed parsing the signature\n");
95-
return 1;
96+
return EXIT_FAILURE;
9697
}
9798

9899
/* Deserialize the public key. This will return 0 if the public key can't be parsed correctly. */
99100
if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, compressed_pubkey, sizeof(compressed_pubkey))) {
100101
printf("Failed parsing the public key\n");
101-
return 1;
102+
return EXIT_FAILURE;
102103
}
103104

104105
/* Verify a signature. This will return 1 if it's valid and 0 if it's not. */
@@ -133,5 +134,5 @@ int main(void) {
133134
* will remove any writes that aren't used. */
134135
secure_erase(seckey, sizeof(seckey));
135136

136-
return 0;
137+
return EXIT_SUCCESS;
137138
}

examples/ellswift.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stdio.h>
1616
#include <assert.h>
1717
#include <string.h>
18+
#include <stdlib.h>
1819

1920
#include <secp256k1.h>
2021
#include <secp256k1_ellswift.h>
@@ -38,7 +39,7 @@ int main(void) {
3839
ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
3940
if (!fill_random(randomize, sizeof(randomize))) {
4041
printf("Failed to generate randomness\n");
41-
return 1;
42+
return EXIT_FAILURE;
4243
}
4344
/* Randomizing the context is recommended to protect against side-channel
4445
* leakage. See `secp256k1_context_randomize` in secp256k1.h for more
@@ -53,19 +54,19 @@ int main(void) {
5354
* is negligible with a properly functioning random number generator. */
5455
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
5556
printf("Failed to generate randomness\n");
56-
return 1;
57+
return EXIT_FAILURE;
5758
}
5859
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
5960
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
60-
return 1;
61+
return EXIT_FAILURE;
6162
}
6263

6364
/* Generate ElligatorSwift public keys. This should never fail with valid context and
6465
verified secret keys. Note that providing additional randomness (fourth parameter) is
6566
optional, but recommended. */
6667
if (!fill_random(auxrand1, sizeof(auxrand1)) || !fill_random(auxrand2, sizeof(auxrand2))) {
6768
printf("Failed to generate randomness\n");
68-
return 1;
69+
return EXIT_FAILURE;
6970
}
7071
return_val = secp256k1_ellswift_create(ctx, ellswift_pubkey1, seckey1, auxrand1);
7172
assert(return_val);
@@ -118,5 +119,5 @@ int main(void) {
118119
secure_erase(shared_secret1, sizeof(shared_secret1));
119120
secure_erase(shared_secret2, sizeof(shared_secret2));
120121

121-
return 0;
122+
return EXIT_SUCCESS;
122123
}

examples/musig.c

+23-22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdio.h>
1515
#include <assert.h>
1616
#include <string.h>
17+
#include <stdlib.h>
1718

1819
#include <secp256k1.h>
1920
#include <secp256k1_extrakeys.h>
@@ -41,18 +42,18 @@ static int create_keypair(const secp256k1_context* ctx, struct signer_secrets *s
4142
while (1) {
4243
if (!fill_random(seckey, sizeof(seckey))) {
4344
printf("Failed to generate randomness\n");
44-
return 0;
45+
return EXIT_FAILURE;
4546
}
4647
if (secp256k1_keypair_create(ctx, &signer_secrets->keypair, seckey)) {
4748
break;
4849
}
4950
}
5051
if (!secp256k1_keypair_pub(ctx, &signer->pubkey, &signer_secrets->keypair)) {
51-
return 0;
52+
return EXIT_FAILURE;
5253
}
5354

5455
secure_erase(seckey, sizeof(seckey));
55-
return 1;
56+
return EXIT_SUCCESS;
5657
}
5758

5859
/* Tweak the pubkey corresponding to the provided keyagg cache, update the cache
@@ -70,7 +71,7 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
7071
/* Plain tweaking which, for example, allows deriving multiple child
7172
* public keys from a single aggregate key using BIP32 */
7273
if (!secp256k1_musig_pubkey_ec_tweak_add(ctx, NULL, cache, plain_tweak)) {
73-
return 0;
74+
return EXIT_FAILURE;
7475
}
7576
/* Note that we did not provide an output_pk argument, because the
7677
* resulting pk is also saved in the cache and so if one is just interested
@@ -81,7 +82,7 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
8182

8283
/* Xonly tweaking which, for example, allows creating Taproot commitments */
8384
if (!secp256k1_musig_pubkey_xonly_tweak_add(ctx, &output_pk, cache, xonly_tweak)) {
84-
return 0;
85+
return EXIT_FAILURE;
8586
}
8687
/* Note that if we wouldn't care about signing, we can arrive at the same
8788
* output_pk by providing the untweaked public key to
@@ -93,9 +94,9 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
9394
* `pk_parity` output argument; we would need it if we would have to open
9495
* the Taproot commitment. */
9596
if (!secp256k1_xonly_pubkey_from_pubkey(ctx, agg_pk, NULL, &output_pk)) {
96-
return 0;
97+
return EXIT_FAILURE;
9798
}
98-
return 1;
99+
return EXIT_FAILURE;
99100
}
100101

101102
/* Sign a message hash with the given key pairs and store the result in sig */
@@ -114,15 +115,15 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
114115
* is unique for every call of secp256k1_musig_nonce_gen. Otherwise
115116
* it's trivial for an attacker to extract the secret key! */
116117
if (!fill_random(session_secrand, sizeof(session_secrand))) {
117-
return 0;
118+
return EXIT_FAILURE;
118119
}
119120
if (!secp256k1_keypair_sec(ctx, seckey, &signer_secrets[i].keypair)) {
120-
return 0;
121+
return EXIT_FAILURE;
121122
}
122123
/* Initialize session and create secret nonce for signing and public
123124
* nonce to send to the other signers. */
124125
if (!secp256k1_musig_nonce_gen(ctx, &signer_secrets[i].secnonce, &signer[i].pubnonce, session_secrand, seckey, &signer[i].pubkey, msg32, NULL, NULL)) {
125-
return 0;
126+
return EXIT_FAILURE;
126127
}
127128
pubnonces[i] = &signer[i].pubnonce;
128129

@@ -133,21 +134,21 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
133134
* coordinator. The coordinator runs secp256k1_musig_nonce_agg and sends
134135
* agg_pubnonce to each signer */
135136
if (!secp256k1_musig_nonce_agg(ctx, &agg_pubnonce, pubnonces, N_SIGNERS)) {
136-
return 0;
137+
return EXIT_FAILURE;
137138
}
138139

139140
/* Every signer creates a partial signature */
140141
for (i = 0; i < N_SIGNERS; i++) {
141142
/* Initialize the signing session by processing the aggregate nonce */
142143
if (!secp256k1_musig_nonce_process(ctx, &session, &agg_pubnonce, msg32, cache)) {
143-
return 0;
144+
return EXIT_FAILURE;
144145
}
145146
/* partial_sign will clear the secnonce by setting it to 0. That's because
146147
* you must _never_ reuse the secnonce (or use the same session_secrand to
147148
* create a secnonce). If you do, you effectively reuse the nonce and
148149
* leak the secret key. */
149150
if (!secp256k1_musig_partial_sign(ctx, &signer[i].partial_sig, &signer_secrets[i].secnonce, &signer_secrets[i].keypair, cache, &session)) {
150-
return 0;
151+
return EXIT_FAILURE;
151152
}
152153
partial_sigs[i] = &signer[i].partial_sig;
153154
}
@@ -166,7 +167,7 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
166167
* sigs if it does not work.
167168
*/
168169
if (!secp256k1_musig_partial_sig_verify(ctx, &signer[i].partial_sig, &signer[i].pubnonce, &signer[i].pubkey, cache, &session)) {
169-
return 0;
170+
return EXIT_FAILURE;
170171
}
171172
}
172173
return secp256k1_musig_partial_sig_agg(ctx, sig64, &session, partial_sigs, N_SIGNERS);
@@ -188,9 +189,9 @@ int main(void) {
188189
printf("Creating key pairs......");
189190
fflush(stdout);
190191
for (i = 0; i < N_SIGNERS; i++) {
191-
if (!create_keypair(ctx, &signer_secrets[i], &signers[i])) {
192+
if (EXIT_FAILURE == create_keypair(ctx, &signer_secrets[i], &signers[i])) {
192193
printf("FAILED\n");
193-
return 1;
194+
return EXIT_FAILURE;
194195
}
195196
pubkeys_ptr[i] = &signers[i].pubkey;
196197
}
@@ -205,7 +206,7 @@ int main(void) {
205206
fflush(stdout);
206207
if (!secp256k1_ec_pubkey_sort(ctx, pubkeys_ptr, N_SIGNERS)) {
207208
printf("FAILED\n");
208-
return 1;
209+
return EXIT_FAILURE;
209210
}
210211
printf("ok\n");
211212

@@ -216,29 +217,29 @@ int main(void) {
216217
* while providing a non-NULL agg_pk argument. */
217218
if (!secp256k1_musig_pubkey_agg(ctx, NULL, &cache, pubkeys_ptr, N_SIGNERS)) {
218219
printf("FAILED\n");
219-
return 1;
220+
return EXIT_FAILURE;
220221
}
221222
printf("ok\n");
222223
printf("Tweaking................");
223224
fflush(stdout);
224225
/* Optionally tweak the aggregate key */
225226
if (!tweak(ctx, &agg_pk, &cache)) {
226227
printf("FAILED\n");
227-
return 1;
228+
return EXIT_FAILURE;
228229
}
229230
printf("ok\n");
230231
printf("Signing message.........");
231232
fflush(stdout);
232233
if (!sign(ctx, signer_secrets, signers, &cache, msg, sig)) {
233234
printf("FAILED\n");
234-
return 1;
235+
return EXIT_FAILURE;
235236
}
236237
printf("ok\n");
237238
printf("Verifying signature.....");
238239
fflush(stdout);
239240
if (!secp256k1_schnorrsig_verify(ctx, sig, msg, 32, &agg_pk)) {
240241
printf("FAILED\n");
241-
return 1;
242+
return EXIT_FAILURE;
242243
}
243244
printf("ok\n");
244245

@@ -253,5 +254,5 @@ int main(void) {
253254
secure_erase(&signer_secrets[i], sizeof(signer_secrets[i]));
254255
}
255256
secp256k1_context_destroy(ctx);
256-
return 0;
257+
return EXIT_SUCCESS;
257258
}

examples/schnorr.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <assert.h>
1212
#include <string.h>
13+
#include <stdlib.h>
1314

1415
#include <secp256k1.h>
1516
#include <secp256k1_extrakeys.h>
@@ -34,7 +35,7 @@ int main(void) {
3435
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
3536
if (!fill_random(randomize, sizeof(randomize))) {
3637
printf("Failed to generate randomness\n");
37-
return 1;
38+
return EXIT_FAILURE;
3839
}
3940
/* Randomizing the context is recommended to protect against side-channel
4041
* leakage See `secp256k1_context_randomize` in secp256k1.h for more
@@ -48,13 +49,13 @@ int main(void) {
4849
* is negligible with a properly functioning random number generator. */
4950
if (!fill_random(seckey, sizeof(seckey))) {
5051
printf("Failed to generate randomness\n");
51-
return 1;
52+
return EXIT_FAILURE;
5253
}
5354
/* Try to create a keypair with a valid context, it should only fail if
5455
* the secret key is zero or out of range. */
5556
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
5657
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
57-
return 1;
58+
return EXIT_FAILURE;
5859
}
5960

6061
/* Extract the X-only public key from the keypair. We pass NULL for
@@ -91,7 +92,7 @@ int main(void) {
9192
/* Generate 32 bytes of randomness to use with BIP-340 schnorr signing. */
9293
if (!fill_random(auxiliary_rand, sizeof(auxiliary_rand))) {
9394
printf("Failed to generate randomness\n");
94-
return 1;
95+
return EXIT_FAILURE;
9596
}
9697

9798
/* Generate a Schnorr signature.
@@ -111,7 +112,7 @@ int main(void) {
111112
* be parsed correctly */
112113
if (!secp256k1_xonly_pubkey_parse(ctx, &pubkey, serialized_pubkey)) {
113114
printf("Failed parsing the public key\n");
114-
return 1;
115+
return EXIT_FAILURE;
115116
}
116117

117118
/* Compute the tagged hash on the received messages using the same tag as the signer. */
@@ -150,5 +151,5 @@ int main(void) {
150151
* Here we are preventing these writes from being optimized out, as any good compiler
151152
* will remove any writes that aren't used. */
152153
secure_erase(seckey, sizeof(seckey));
153-
return 0;
154+
return EXIT_SUCCESS;
154155
}

0 commit comments

Comments
 (0)