Skip to content

Commit

Permalink
Improve examples: remove key generation loops
Browse files Browse the repository at this point in the history
improving examples bitcoin-core#2: fixing my mistakes; changing comments

improving examples bitcoin-core#3: changing comments

fixing syntax - adjustment to schnorr examples

fixing comments

fix schnorr signature tests
  • Loading branch information
Cheapshot003 committed Sep 3, 2024
1 parent 1988855 commit 3e8170c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
22 changes: 10 additions & 12 deletions examples/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ int main(void) {
assert(return_val);

/*** Key Generation ***/

/* If the secret key is zero or out of range (bigger than secp256k1's
* order), we try to sample a new key. Note that the probability of this
* happening is negligible. */
while (1) {
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
printf("Failed to generate randomness\n");
return 1;
}
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
break;
}
/* If the secret key is zero or out of range (greater than secp256k1's
* order), we return 1. Note that the probability of this occurring
* is negligible with a properly functioning random number generator. */
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
printf("Failed to generate randomness\n");
return 1;
}
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
return 1;
}

/* Public key creation using a valid context with a verified secret key should never fail */
Expand Down
22 changes: 10 additions & 12 deletions examples/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,16 @@ int main(void) {
assert(return_val);

/*** Key Generation ***/

/* If the secret key is zero or out of range (bigger than secp256k1's
* order), we try to sample a new key. Note that the probability of this
* happening is negligible. */
while (1) {
if (!fill_random(seckey, sizeof(seckey))) {
printf("Failed to generate randomness\n");
return 1;
}
if (secp256k1_ec_seckey_verify(ctx, seckey)) {
break;
}
/* If the secret key is zero or out of range (greater than secp256k1's
* order), we return 1. Note that the probability of this occurring
* is negligible with a properly functioning random number generator. */
if (!fill_random(seckey, sizeof(seckey))) {
printf("Failed to generate randomness\n");
return 1;
}
if (!secp256k1_ec_seckey_verify(ctx, seckey)) {
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
return 1;
}

/* Public key creation using a valid context with a verified secret key should never fail */
Expand Down
21 changes: 10 additions & 11 deletions examples/ellswift.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ int main(void) {

/*** Generate secret keys ***/

/* If the secret key is zero or out of range (bigger than secp256k1's
* order), we try to sample a new key. Note that the probability of this
* happening is negligible. */
while (1) {
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
printf("Failed to generate randomness\n");
return 1;
}
if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) {
break;
}
/* If the secret key is zero or out of range (greater than secp256k1's
* order), we return 1. Note that the probability of this occurring
* is negligible with a properly functioning random number generator. */
if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
printf("Failed to generate randomness\n");
return 1;
}
if (!secp256k1_ec_seckey_verify(ctx, seckey1) || !secp256k1_ec_seckey_verify(ctx, seckey2)) {
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
return 1;
}

/* Generate ElligatorSwift public keys. This should never fail with valid context and
Expand Down
26 changes: 12 additions & 14 deletions examples/schnorr.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ int main(void) {
assert(return_val);

/*** Key Generation ***/

/* If the secret key is zero or out of range (bigger than secp256k1's
* order), we try to sample a new key. Note that the probability of this
* happening is negligible. */
while (1) {
if (!fill_random(seckey, sizeof(seckey))) {
printf("Failed to generate randomness\n");
return 1;
}
/* Try to create a keypair with a valid context, it should only fail if
* the secret key is zero or out of range. */
if (secp256k1_keypair_create(ctx, &keypair, seckey)) {
break;
}
/* If the secret key is zero or out of range (greater than secp256k1's
* order), we return 1. Note that the probability of this occurring
* is negligible with a properly functioning random number generator. */
if (!fill_random(seckey, sizeof(seckey))) {
printf("Failed to generate randomness\n");
return 1;
}
/* Try to create a keypair with a valid context, it should only fail if
* the secret key is zero or out of range. */
if (!secp256k1_keypair_create(ctx, &keypair, seckey)) {
printf("Generated secret key is invalid. This indicates an issue with the random number generator.\n");
return 1;
}

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

0 comments on commit 3e8170c

Please sign in to comment.