Skip to content

Commit c0d9480

Browse files
Merge #1654: use EXIT_ constants over magic numbers for indicating program execution status
13d3896 CONTRIBUTING: mention that `EXIT_` codes should be used (Sebastian Falbesoner) c855581 test, bench, precompute_ecmult: use `EXIT_...` constants for `main` return values (Sebastian Falbesoner) 965393f examples: use `EXIT_...` constants for `main` return values (Sebastian Falbesoner) Pull request description: This simple PR addresses #1609 for all example and internal binaries. Alternative to #1618, which is stale (the author confirmed to me that they are not working on that PR anymore). The last commits adds a suggestion to CONTRIBUTING.md, not sure though if we want to go that far. ACKs for top commit: jonasnick: ACK 13d3896 real-or-random: utACK 13d3896 Tree-SHA512: 513eba4b712ba3d5f23a5fdc51cb27c5347b29bcaba39501345913c220be6f093a41186911032d2ddc898b848de84f05f374b3554ffcf92610728b2a23c0bb36
2 parents 2e3bf13 + 13d3896 commit c0d9480

15 files changed

+62
-50
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ In addition, libsecp256k1 tries to maintain the following coding conventions:
7474
* User-facing comment lines in headers should be limited to 80 chars if possible.
7575
* All identifiers in file scope should start with `secp256k1_`.
7676
* Avoid trailing whitespace.
77+
* Use the constants `EXIT_SUCCESS`/`EXIT_FAILURE` (defined in `stdlib.h`) to indicate program execution status for examples and other binaries.
7778

7879
### Tests
7980

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
}

src/bench.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
***********************************************************************/
66

77
#include <stdio.h>
8+
#include <stdlib.h>
89
#include <string.h>
910

1011
#include "../include/secp256k1.h"
@@ -188,11 +189,11 @@ int main(int argc, char** argv) {
188189
|| have_flag(argc, argv, "--help")
189190
|| have_flag(argc, argv, "help")) {
190191
help(default_iters);
191-
return 0;
192+
return EXIT_SUCCESS;
192193
} else if (invalid_args) {
193194
fprintf(stderr, "./bench: unrecognized argument.\n\n");
194195
help(default_iters);
195-
return 1;
196+
return EXIT_FAILURE;
196197
}
197198
}
198199

@@ -201,23 +202,23 @@ int main(int argc, char** argv) {
201202
if (have_flag(argc, argv, "ecdh")) {
202203
fprintf(stderr, "./bench: ECDH module not enabled.\n");
203204
fprintf(stderr, "Use ./configure --enable-module-ecdh.\n\n");
204-
return 1;
205+
return EXIT_FAILURE;
205206
}
206207
#endif
207208

208209
#ifndef ENABLE_MODULE_RECOVERY
209210
if (have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) {
210211
fprintf(stderr, "./bench: Public key recovery module not enabled.\n");
211212
fprintf(stderr, "Use ./configure --enable-module-recovery.\n\n");
212-
return 1;
213+
return EXIT_FAILURE;
213214
}
214215
#endif
215216

216217
#ifndef ENABLE_MODULE_SCHNORRSIG
217218
if (have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "schnorrsig_sign") || have_flag(argc, argv, "schnorrsig_verify")) {
218219
fprintf(stderr, "./bench: Schnorr signatures module not enabled.\n");
219220
fprintf(stderr, "Use ./configure --enable-module-schnorrsig.\n\n");
220-
return 1;
221+
return EXIT_FAILURE;
221222
}
222223
#endif
223224

@@ -227,7 +228,7 @@ int main(int argc, char** argv) {
227228
have_flag(argc, argv, "ellswift_ecdh")) {
228229
fprintf(stderr, "./bench: ElligatorSwift module not enabled.\n");
229230
fprintf(stderr, "Use ./configure --enable-module-ellswift.\n\n");
230-
return 1;
231+
return EXIT_FAILURE;
231232
}
232233
#endif
233234

@@ -275,5 +276,5 @@ int main(int argc, char** argv) {
275276
run_ellswift_bench(iters, argc, argv);
276277
#endif
277278

278-
return 0;
279+
return EXIT_SUCCESS;
279280
}

src/bench.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static int64_t gettime_i64(void) {
2424
struct timespec tv;
2525
if (!timespec_get(&tv, TIME_UTC)) {
2626
fputs("timespec_get failed!", stderr);
27-
exit(1);
27+
exit(EXIT_FAILURE);
2828
}
2929
return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL;
3030
#else

src/bench_ecmult.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66
#include <stdio.h>
7+
#include <stdlib.h>
78

89
#include "secp256k1.c"
910
#include "../include/secp256k1.h"
@@ -287,7 +288,7 @@ int main(int argc, char **argv) {
287288
|| have_flag(argc, argv, "--help")
288289
|| have_flag(argc, argv, "help")) {
289290
help(argv);
290-
return 0;
291+
return EXIT_SUCCESS;
291292
} else if(have_flag(argc, argv, "pippenger_wnaf")) {
292293
printf("Using pippenger_wnaf:\n");
293294
data.ecmult_multi = secp256k1_ecmult_pippenger_batch_single;
@@ -299,7 +300,7 @@ int main(int argc, char **argv) {
299300
} else {
300301
fprintf(stderr, "%s: unrecognized argument '%s'.\n\n", argv[0], argv[1]);
301302
help(argv);
302-
return 1;
303+
return EXIT_FAILURE;
303304
}
304305
}
305306

@@ -363,5 +364,5 @@ int main(int argc, char **argv) {
363364
free(data.output);
364365
free(data.expected_output);
365366

366-
return(0);
367+
return EXIT_SUCCESS;
367368
}

0 commit comments

Comments
 (0)