Skip to content

Commit fdeda3d

Browse files
committed
examples: do not retry generating seckey randomness in musig
1 parent 01b5893 commit fdeda3d

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

examples/musig.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ 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+
/* If the secret key is zero or out of range (greater than secp256k1's
47+
* order), we fail. Note that the probability of this occurring
48+
* is negligible with a properly functioning random number generator. */
49+
if (secp256k1_keypair_create(ctx, &signer_secrets->keypair, seckey)) {
50+
return 0;
4951
}
5052
if (!secp256k1_keypair_pub(ctx, &signer->pubkey, &signer_secrets->keypair)) {
5153
return 0;

examples/schnorr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main(void) {
5454
* the secret key is zero or out of range. */
5555
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
5656
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
57-
return 1;
57+
return 1;
5858
}
5959

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

include/secp256k1.h

+1-1
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
*

0 commit comments

Comments
 (0)