Skip to content

Commit 18f9b96

Browse files
Merge #1616: examples: do not retry generating seckey randomness in musig
5bab8f6 examples: make key generation doc consistent (Jonas Nick) e890822 examples: do not retry generating seckey randomness in musig (Jonas Nick) 70b6be1 extrakeys: improve doc of keypair_create (don't suggest retry) (Jonas Nick) Pull request description: Follow-up to #1570. ACKs for top commit: real-or-random: utACK 5bab8f6 theStack: ACK 5bab8f6 Tree-SHA512: f29ceda87b0017aa2a2324f23527467c777223c9f7cbe43d814bb1cebfc6f4453b7e11f48a6bc718ae05d7eb9227ceb074adf576e8bb8c28639b47931136ce0a
2 parents 01b5893 + 5bab8f6 commit 18f9b96

File tree

7 files changed

+31
-27
lines changed

7 files changed

+31
-27
lines changed

examples/ecdh.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ int main(void) {
4747
return 1;
4848
}
4949
/* 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. */
50+
* order), we fail. Note that the probability of this occurring is negligible
51+
* with a properly functioning random number generator. */
5252
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
5353
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
5454
return 1;

examples/ecdsa.c

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

5151
/*** Key Generation ***/
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. */
5552
if (!fill_random(seckey, sizeof(seckey))) {
5653
printf("Failed to generate randomness\n");
5754
return 1;
5855
}
56+
/* If the secret key is zero or out of range (greater than secp256k1's
57+
* order), we fail. Note that the probability of this occurring is negligible
58+
* with a properly functioning random number generator. */
5959
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
6060
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
6161
return 1;

examples/ellswift.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ int main(void) {
4747
assert(return_val);
4848

4949
/*** Generate secret keys ***/
50-
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. */
5450
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
5551
printf("Failed to generate randomness\n");
5652
return 1;
5753
}
54+
/* If the secret key is zero or out of range (greater than secp256k1's
55+
* order), we fail. Note that the probability of this occurring is negligible
56+
* with a properly functioning random number generator. */
5857
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
5958
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
6059
return 1;

examples/musig.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ struct signer {
3838
/* Create a key pair, store it in signer_secrets->keypair and signer->pubkey */
3939
static int create_keypair(const secp256k1_context* ctx, struct signer_secrets *signer_secrets, struct signer *signer) {
4040
unsigned char seckey[32];
41-
while (1) {
42-
if (!fill_random(seckey, sizeof(seckey))) {
43-
printf("Failed to generate randomness\n");
44-
return 0;
45-
}
46-
if (secp256k1_keypair_create(ctx, &signer_secrets->keypair, seckey)) {
47-
break;
48-
}
41+
42+
if (!fill_random(seckey, sizeof(seckey))) {
43+
printf("Failed to generate randomness\n");
44+
return 0;
45+
}
46+
/* Try to create a keypair with a valid context. This only fails if the
47+
* secret key is zero or out of range (greater than secp256k1's order). Note
48+
* that the probability of this occurring is negligible with a properly
49+
* functioning random number generator. */
50+
if (!secp256k1_keypair_create(ctx, &signer_secrets->keypair, seckey)) {
51+
return 0;
4952
}
5053
if (!secp256k1_keypair_pub(ctx, &signer->pubkey, &signer_secrets->keypair)) {
5154
return 0;

examples/schnorr.c

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

4545
/*** Key Generation ***/
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. */
4946
if (!fill_random(seckey, sizeof(seckey))) {
5047
printf("Failed to generate randomness\n");
5148
return 1;
5249
}
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. */
50+
/* Try to create a keypair with a valid context. This only fails if the
51+
* secret key is zero or out of range (greater than secp256k1's order). Note
52+
* that the probability of this occurring is negligible with a properly
53+
* functioning random number generator. */
5554
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
5655
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
57-
return 1;
56+
return 1;
5857
}
5958

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

include/secp256k1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ SECP256K1_API int secp256k1_ecdsa_sign(
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. However, if it does happen it should
687+
* invalid secret key is negligible. However, if it does happen it should
688688
* be assumed that the randomness source is severely broken and there should
689689
* be no retry.
690690
*

include/secp256k1_extrakeys.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_
155155
const unsigned char *tweak32
156156
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
157157

158-
/** Compute the keypair for a secret key.
158+
/** Compute the keypair for a valid secret key.
159159
*
160-
* Returns: 1: secret was valid, keypair is ready to use
161-
* 0: secret was invalid, try again with a different secret
160+
* See the documentation of `secp256k1_ec_seckey_verify` for more information
161+
* about the validity of secret keys.
162+
*
163+
* Returns: 1: secret key is valid
164+
* 0: secret key is invalid
162165
* Args: ctx: pointer to a context object (not secp256k1_context_static).
163166
* Out: keypair: pointer to the created keypair.
164167
* In: seckey: pointer to a 32-byte secret key.

0 commit comments

Comments
 (0)