Skip to content

Commit 01b5893

Browse files
committed
Merge #1599: #1570 improve examples: remove key generation loop
cd4f84f Improve examples/documentation: remove key generation loops (cheapshot003) Pull request description: ACKs for top commit: real-or-random: utACK cd4f84f jonasnick: ACK cd4f84f Tree-SHA512: 242ab99c36302b539fc95421142c3eec5ccfa2cf918989457886338febde45a33b1794e0f08e7a632747bc21cbf5c47b7361fd9a28b9a1c6dff7caecf7b31a9f
2 parents a88aa93 + cd4f84f commit 01b5893

File tree

5 files changed

+46
-51
lines changed

5 files changed

+46
-51
lines changed

examples/ecdh.c

+10-12
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 (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
46+
printf("Failed to generate randomness\n");
47+
return 1;
48+
}
49+
/* If the secret key is zero or out of range (greater than secp256k1's
50+
* order), we fail. Note that the probability of this occurring
51+
* is negligible with a properly functioning random number generator. */
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

+10-12
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

+10-11
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

+12-14
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

include/secp256k1.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,14 @@ SECP256K1_API int secp256k1_ecdsa_sign(
679679
const void *ndata
680680
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
681681

682-
/** Verify an ECDSA secret key.
682+
/** Verify an elliptic curve secret key.
683683
*
684684
* A secret key is valid if it is not 0 and less than the secp256k1 curve order
685685
* when interpreted as an integer (most significant byte first). The
686686
* probability of choosing a 32-byte string uniformly at random which is an
687-
* invalid secret key is negligible.
687+
* invalid secret key is negligible. However, if it does happen it should
688+
* be assumed that the randomness source is severely broken and there should
689+
* be no retry.
688690
*
689691
* Returns: 1: secret key is valid
690692
* 0: secret key is invalid

0 commit comments

Comments
 (0)