Skip to content

Commit d9dca14

Browse files
committed
improving examples #2: fixing my mistakes; changing comments
1 parent 9ac3bcc commit d9dca14

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

examples/ecdh.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ int main(void) {
5050
printf("Failed to generate randomness\n");
5151
return 1;
5252
}
53-
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
54-
break;
53+
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
54+
return 1;
5555
}
5656

5757
/* Public key creation using a valid context with a verified secret key should never fail */

examples/ecdsa.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ int main(void) {
5151
/*** Key Generation ***/
5252

5353
/* If the secret key is zero or out of range (bigger than secp256k1's
54-
* order), we try to sample a new key. Note that the probability of this
55-
* happening is negligible. */
54+
* order), we return 1. Note that the probability of this
55+
* happening is negligible, though it could indicate a faulty RNG */
5656
if (!fill_random(seckey, sizeof(seckey))) {
5757
printf("Failed to generate randomness\n");
5858
return 1;
5959
}
60-
if (secp256k1_ec_seckey_verify(ctx, seckey)) {
61-
break;
60+
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
61+
return 1;
6262
}
6363

6464
/* Public key creation using a valid context with a verified secret key should never fail */

examples/ellswift.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ int main(void) {
4949
/*** Generate secret keys ***/
5050

5151
/* If the secret key is zero or out of range (bigger than secp256k1's
52-
* order), we try to sample a new key. Note that the probability of this
53-
* happening is negligible. */
52+
* order), we return 1. Note that the probability of this
53+
* happening is negligible, though it could indicate a faulty RNG */
5454
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
5555
printf("Failed to generate randomness\n");
5656
return 1;
5757
}
58-
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
59-
break;
58+
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
59+
printf("Generated secret key is invalid. This could indicate an issue with the random number generator.\n")
60+
return 1;
6061
}
6162

6263
/* Generate ElligatorSwift public keys. This should never fail with valid context and

0 commit comments

Comments
 (0)