Skip to content

Commit 563b3be

Browse files
theStackSebastian Falbesoner
authored and
Sebastian Falbesoner
committed
examples: use EXIT_... constants for main return values
Fixes issue bitcoin-core#1609.
1 parent 2e3bf13 commit 563b3be

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

examples/ecdh.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*************************************************************************/
99

1010
#include <stdio.h>
11+
#include <stdlib.h>
1112
#include <assert.h>
1213
#include <string.h>
1314

@@ -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 is negligible
5152
* 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

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*************************************************************************/
99

1010
#include <stdio.h>
11+
#include <stdlib.h>
1112
#include <assert.h>
1213
#include <string.h>
1314

@@ -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
@@ -51,14 +52,14 @@ int main(void) {
5152
/*** Key Generation ***/
5253
if (!fill_random(seckey, sizeof(seckey))) {
5354
printf("Failed to generate randomness\n");
54-
return 1;
55+
return EXIT_FAILURE;
5556
}
5657
/* If the secret key is zero or out of range (greater than secp256k1's
5758
* order), we fail. Note that the probability of this occurring is negligible
5859
* with a properly functioning random number generator. */
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
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#include <stdio.h>
16+
#include <stdlib.h>
1617
#include <assert.h>
1718
#include <string.h>
1819

@@ -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
@@ -49,22 +50,22 @@ int main(void) {
4950
/*** Generate secret keys ***/
5051
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
5152
printf("Failed to generate randomness\n");
52-
return 1;
53+
return EXIT_FAILURE;
5354
}
5455
/* If the secret key is zero or out of range (greater than secp256k1's
5556
* order), we fail. Note that the probability of this occurring is negligible
5657
* with a properly functioning random number generator. */
5758
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
5859
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
59-
return 1;
60+
return EXIT_FAILURE;
6061
}
6162

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

120-
return 0;
121+
return EXIT_SUCCESS;
121122
}

examples/musig.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include <stdio.h>
15+
#include <stdlib.h>
1516
#include <assert.h>
1617
#include <string.h>
1718

@@ -193,7 +194,7 @@ int main(void) {
193194
for (i = 0; i < N_SIGNERS; i++) {
194195
if (!create_keypair(ctx, &signer_secrets[i], &signers[i])) {
195196
printf("FAILED\n");
196-
return 1;
197+
return EXIT_FAILURE;
197198
}
198199
pubkeys_ptr[i] = &signers[i].pubkey;
199200
}
@@ -208,7 +209,7 @@ int main(void) {
208209
fflush(stdout);
209210
if (!secp256k1_ec_pubkey_sort(ctx, pubkeys_ptr, N_SIGNERS)) {
210211
printf("FAILED\n");
211-
return 1;
212+
return EXIT_FAILURE;
212213
}
213214
printf("ok\n");
214215

@@ -219,29 +220,29 @@ int main(void) {
219220
* while providing a non-NULL agg_pk argument. */
220221
if (!secp256k1_musig_pubkey_agg(ctx, NULL, &cache, pubkeys_ptr, N_SIGNERS)) {
221222
printf("FAILED\n");
222-
return 1;
223+
return EXIT_FAILURE;
223224
}
224225
printf("ok\n");
225226
printf("Tweaking................");
226227
fflush(stdout);
227228
/* Optionally tweak the aggregate key */
228229
if (!tweak(ctx, &agg_pk, &cache)) {
229230
printf("FAILED\n");
230-
return 1;
231+
return EXIT_FAILURE;
231232
}
232233
printf("ok\n");
233234
printf("Signing message.........");
234235
fflush(stdout);
235236
if (!sign(ctx, signer_secrets, signers, &cache, msg, sig)) {
236237
printf("FAILED\n");
237-
return 1;
238+
return EXIT_FAILURE;
238239
}
239240
printf("ok\n");
240241
printf("Verifying signature.....");
241242
fflush(stdout);
242243
if (!secp256k1_schnorrsig_verify(ctx, sig, msg, 32, &agg_pk)) {
243244
printf("FAILED\n");
244-
return 1;
245+
return EXIT_FAILURE;
245246
}
246247
printf("ok\n");
247248

@@ -256,5 +257,5 @@ int main(void) {
256257
secure_erase(&signer_secrets[i], sizeof(signer_secrets[i]));
257258
}
258259
secp256k1_context_destroy(ctx);
259-
return 0;
260+
return EXIT_SUCCESS;
260261
}

examples/schnorr.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*************************************************************************/
99

1010
#include <stdio.h>
11+
#include <stdlib.h>
1112
#include <assert.h>
1213
#include <string.h>
1314

@@ -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
@@ -45,15 +46,15 @@ int main(void) {
4546
/*** Key Generation ***/
4647
if (!fill_random(seckey, sizeof(seckey))) {
4748
printf("Failed to generate randomness\n");
48-
return 1;
49+
return EXIT_FAILURE;
4950
}
5051
/* Try to create a keypair with a valid context. This only fails if the
5152
* secret key is zero or out of range (greater than secp256k1's order). Note
5253
* that the probability of this occurring is negligible with a properly
5354
* functioning random number generator. */
5455
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
5556
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
56-
return 1;
57+
return EXIT_FAILURE;
5758
}
5859

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

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

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

0 commit comments

Comments
 (0)