Skip to content

Commit 3e8170c

Browse files
committed
Improve examples: remove key generation loops
improving examples #2: fixing my mistakes; changing comments improving examples #3: changing comments fixing syntax - adjustment to schnorr examples fixing comments fix schnorr signature tests
1 parent 1988855 commit 3e8170c

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

examples/ecdh.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ int main(void) {
4242
assert(return_val);
4343

4444
/*** Key Generation ***/
45-
46-
/* If the secret key is zero or out of range (bigger than secp256k1's
47-
* order), we try to sample a new key. Note that the probability of this
48-
* happening is negligible. */
49-
while (1) {
50-
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
51-
printf("Failed to generate randomness\n");
52-
return 1;
53-
}
54-
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
55-
break;
56-
}
45+
/* If the secret key is zero or out of range (greater than secp256k1's
46+
* order), we return 1. Note that the probability of this occurring
47+
* is negligible with a properly functioning random number generator. */
48+
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
49+
printf("Failed to generate randomness\n");
50+
return 1;
51+
}
52+
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
53+
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
54+
return 1;
5755
}
5856

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

examples/ecdsa.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,16 @@ int main(void) {
4949
assert(return_val);
5050

5151
/*** Key Generation ***/
52-
53-
/* 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. */
56-
while (1) {
57-
if (!fill_random(seckey, sizeof(seckey))) {
58-
printf("Failed to generate randomness\n");
59-
return 1;
60-
}
61-
if (secp256k1_ec_seckey_verify(ctx, seckey)) {
62-
break;
63-
}
52+
/* 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+
* is negligible with a properly functioning random number generator. */
55+
if (!fill_random(seckey, sizeof(seckey))) {
56+
printf("Failed to generate randomness\n");
57+
return 1;
58+
}
59+
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
60+
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
61+
return 1;
6462
}
6563

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

examples/ellswift.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ int main(void) {
4848

4949
/*** Generate secret keys ***/
5050

51-
/* 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. */
54-
while (1) {
55-
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
56-
printf("Failed to generate randomness\n");
57-
return 1;
58-
}
59-
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
60-
break;
61-
}
51+
/* If the secret key is zero or out of range (greater than secp256k1's
52+
* order), we return 1. Note that the probability of this occurring
53+
* is negligible with a properly functioning random number generator. */
54+
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
55+
printf("Failed to generate randomness\n");
56+
return 1;
57+
}
58+
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
59+
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
60+
return 1;
6261
}
6362

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

examples/schnorr.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,18 @@ int main(void) {
4343
assert(return_val);
4444

4545
/*** Key Generation ***/
46-
47-
/* If the secret key is zero or out of range (bigger than secp256k1's
48-
* order), we try to sample a new key. Note that the probability of this
49-
* happening is negligible. */
50-
while (1) {
51-
if (!fill_random(seckey, sizeof(seckey))) {
52-
printf("Failed to generate randomness\n");
53-
return 1;
54-
}
55-
/* Try to create a keypair with a valid context, it should only fail if
56-
* the secret key is zero or out of range. */
57-
if (secp256k1_keypair_create(ctx, &keypair, seckey)) {
58-
break;
59-
}
46+
/* If the secret key is zero or out of range (greater than secp256k1's
47+
* order), we return 1. Note that the probability of this occurring
48+
* is negligible with a properly functioning random number generator. */
49+
if (!fill_random(seckey, sizeof(seckey))) {
50+
printf("Failed to generate randomness\n");
51+
return 1;
52+
}
53+
/* Try to create a keypair with a valid context, it should only fail if
54+
* the secret key is zero or out of range. */
55+
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
56+
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
57+
return 1;
6058
}
6159

6260
/* Extract the X-only public key from the keypair. We pass NULL for

0 commit comments

Comments
 (0)