Skip to content

Commit

Permalink
Improve examples: remove key generation loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheapshot003 committed Aug 29, 2024
1 parent 1988855 commit 9ac3bcc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
14 changes: 6 additions & 8 deletions examples/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ int main(void) {
/* 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 (!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;
}

/* Public key creation using a valid context with a verified secret key should never fail */
Expand Down
14 changes: 6 additions & 8 deletions examples/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ int main(void) {
/* 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 (!fill_random(seckey, sizeof(seckey))) {
printf("Failed to generate randomness\n");
return 1;
}
if (secp256k1_ec_seckey_verify(ctx, seckey)) {
break;
}

/* Public key creation using a valid context with a verified secret key should never fail */
Expand Down
14 changes: 6 additions & 8 deletions examples/ellswift.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ int main(void) {
/* 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 (!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;
}

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

0 comments on commit 9ac3bcc

Please sign in to comment.