From 9ac3bcc776e01c9e5a353cf3df8f00066a8242e1 Mon Sep 17 00:00:00 2001 From: cheapshot003 Date: Thu, 29 Aug 2024 22:24:20 +0200 Subject: [PATCH] Improve examples: remove key generation loops --- examples/ecdh.c | 14 ++++++-------- examples/ecdsa.c | 14 ++++++-------- examples/ellswift.c | 14 ++++++-------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/examples/ecdh.c b/examples/ecdh.c index d71fd2f604..fe2a62c065 100644 --- a/examples/ecdh.c +++ b/examples/ecdh.c @@ -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 */ diff --git a/examples/ecdsa.c b/examples/ecdsa.c index d5c4613d9c..1062e4b464 100644 --- a/examples/ecdsa.c +++ b/examples/ecdsa.c @@ -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 */ diff --git a/examples/ellswift.c b/examples/ellswift.c index 52be7eebfb..d413980578 100644 --- a/examples/ellswift.c +++ b/examples/ellswift.c @@ -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