File tree Expand file tree Collapse file tree 4 files changed +42
-49
lines changed Expand file tree Collapse file tree 4 files changed +42
-49
lines changed Original file line number Diff line number Diff line change @@ -42,18 +42,16 @@ int main(void) {
4242 assert (return_val );
4343
4444 /*** Key Generation ***/
45-
46- /* If the secret key is zero or out of range (bigger than secp256k1's
47- * order), we try to sample a new key. Note that the probability of this
48- * happening is negligible. */
49- while (1 ) {
50- if (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
51- printf ("Failed to generate randomness\n" );
52- return 1 ;
53- }
54- if (secp256k1_ec_seckey_verify (ctx , seckey1 ) && secp256k1_ec_seckey_verify (ctx , seckey2 )) {
55- break ;
56- }
45+ /* If the secret key is zero or out of range (greater than secp256k1's
46+ * order), we return 1. Note that the probability of this occurring
47+ * is negligible with a properly functioning random number generator. */
48+ if (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
49+ printf ("Failed to generate randomness\n" );
50+ return 1 ;
51+ }
52+ if (!secp256k1_ec_seckey_verify (ctx , seckey1 ) || !secp256k1_ec_seckey_verify (ctx , seckey2 )) {
53+ printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
54+ return 1 ;
5755 }
5856
5957 /* Public key creation using a valid context with a verified secret key should never fail */
Original file line number Diff line number Diff line change @@ -49,18 +49,16 @@ int main(void) {
4949 assert (return_val );
5050
5151 /*** Key Generation ***/
52-
53- /* If the secret key is zero or out of range (bigger than secp256k1's
54- * order), we try to sample a new key. Note that the probability of this
55- * happening is negligible. */
56- while (1 ) {
57- if (!fill_random (seckey , sizeof (seckey ))) {
58- printf ("Failed to generate randomness\n" );
59- return 1 ;
60- }
61- if (secp256k1_ec_seckey_verify (ctx , seckey )) {
62- break ;
63- }
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. */
55+ if (!fill_random (seckey , sizeof (seckey ))) {
56+ printf ("Failed to generate randomness\n" );
57+ return 1 ;
58+ }
59+ if (!secp256k1_ec_seckey_verify (ctx , seckey )) {
60+ printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
61+ return 1 ;
6462 }
6563
6664 /* Public key creation using a valid context with a verified secret key should never fail */
Original file line number Diff line number Diff line change @@ -48,17 +48,16 @@ int main(void) {
4848
4949 /*** Generate secret keys ***/
5050
51- /* If the secret key is zero or out of range (bigger than secp256k1's
52- * order), we try to sample a new key. Note that the probability of this
53- * happening is negligible. */
54- while (1 ) {
55- if (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
56- printf ("Failed to generate randomness\n" );
57- return 1 ;
58- }
59- if (secp256k1_ec_seckey_verify (ctx , seckey1 ) && secp256k1_ec_seckey_verify (ctx , seckey2 )) {
60- break ;
61- }
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. */
54+ if (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
55+ printf ("Failed to generate randomness\n" );
56+ return 1 ;
57+ }
58+ if (!secp256k1_ec_seckey_verify (ctx , seckey1 ) || !secp256k1_ec_seckey_verify (ctx , seckey2 )) {
59+ printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
60+ return 1 ;
6261 }
6362
6463 /* Generate ElligatorSwift public keys. This should never fail with valid context and
Original file line number Diff line number Diff line change @@ -43,20 +43,18 @@ int main(void) {
4343 assert (return_val );
4444
4545 /*** Key Generation ***/
46-
47- /* If the secret key is zero or out of range (bigger than secp256k1's
48- * order), we try to sample a new key. Note that the probability of this
49- * happening is negligible. */
50- while (1 ) {
51- if (!fill_random (seckey , sizeof (seckey ))) {
52- printf ("Failed to generate randomness\n" );
53- return 1 ;
54- }
55- /* Try to create a keypair with a valid context, it should only fail if
56- * the secret key is zero or out of range. */
57- if (secp256k1_keypair_create (ctx , & keypair , seckey )) {
58- break ;
59- }
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. */
49+ if (!fill_random (seckey , sizeof (seckey ))) {
50+ printf ("Failed to generate randomness\n" );
51+ return 1 ;
52+ }
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. */
55+ if (!secp256k1_keypair_create (ctx , & keypair , seckey )) {
56+ printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
57+ return 1 ;
6058 }
6159
6260 /* Extract the X-only public key from the keypair. We pass NULL for
You can’t perform that action at this time.
0 commit comments