File tree Expand file tree Collapse file tree 7 files changed +31
-27
lines changed Expand file tree Collapse file tree 7 files changed +31
-27
lines changed Original file line number Diff line number Diff line change @@ -47,8 +47,8 @@ int main(void) {
4747 return 1 ;
4848 }
4949 /* If the secret key is zero or out of range (greater than secp256k1's
50- * order), we fail. Note that the probability of this occurring
51- * is negligible with a properly functioning random number generator. */
50+ * order), we fail. Note that the probability of this occurring is negligible
51+ * with a properly functioning random number generator. */
5252 if (!secp256k1_ec_seckey_verify (ctx , seckey1 ) || !secp256k1_ec_seckey_verify (ctx , seckey2 )) {
5353 printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
5454 return 1 ;
Original file line number Diff line number Diff line change @@ -49,13 +49,13 @@ int main(void) {
4949 assert (return_val );
5050
5151 /*** Key Generation ***/
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. */
5552 if (!fill_random (seckey , sizeof (seckey ))) {
5653 printf ("Failed to generate randomness\n" );
5754 return 1 ;
5855 }
56+ /* If the secret key is zero or out of range (greater than secp256k1's
57+ * order), we fail. Note that the probability of this occurring is negligible
58+ * with a properly functioning random number generator. */
5959 if (!secp256k1_ec_seckey_verify (ctx , seckey )) {
6060 printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
6161 return 1 ;
Original file line number Diff line number Diff line change @@ -47,14 +47,13 @@ int main(void) {
4747 assert (return_val );
4848
4949 /*** Generate secret keys ***/
50-
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. */
5450 if (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
5551 printf ("Failed to generate randomness\n" );
5652 return 1 ;
5753 }
54+ /* If the secret key is zero or out of range (greater than secp256k1's
55+ * order), we fail. Note that the probability of this occurring is negligible
56+ * with a properly functioning random number generator. */
5857 if (!secp256k1_ec_seckey_verify (ctx , seckey1 ) || !secp256k1_ec_seckey_verify (ctx , seckey2 )) {
5958 printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
6059 return 1 ;
Original file line number Diff line number Diff line change @@ -38,14 +38,17 @@ struct signer {
3838/* Create a key pair, store it in signer_secrets->keypair and signer->pubkey */
3939static 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+ /* Try to create a keypair with a valid context. This only fails if the
47+ * secret key is zero or out of range (greater than secp256k1's order). Note
48+ * that the probability of this occurring is negligible with a properly
49+ * functioning random number generator. */
50+ if (!secp256k1_keypair_create (ctx , & signer_secrets -> keypair , seckey )) {
51+ return 0 ;
4952 }
5053 if (!secp256k1_keypair_pub (ctx , & signer -> pubkey , & signer_secrets -> keypair )) {
5154 return 0 ;
Original file line number Diff line number Diff line change @@ -43,18 +43,17 @@ int main(void) {
4343 assert (return_val );
4444
4545 /*** Key Generation ***/
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. */
4946 if (!fill_random (seckey , sizeof (seckey ))) {
5047 printf ("Failed to generate randomness\n" );
5148 return 1 ;
5249 }
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. */
50+ /* Try to create a keypair with a valid context. This only fails if the
51+ * secret key is zero or out of range (greater than secp256k1's order). Note
52+ * that the probability of this occurring is negligible with a properly
53+ * functioning random number generator. */
5554 if (!secp256k1_keypair_create (ctx , & keypair , seckey )) {
5655 printf ("Generated secret key is invalid. This indicates an issue with the random number generator.\n" );
57- return 1 ;
56+ return 1 ;
5857 }
5958
6059 /* Extract the X-only public key from the keypair. We pass NULL for
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change @@ -155,10 +155,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_
155155 const unsigned char * tweak32
156156) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (4 ) SECP256K1_ARG_NONNULL (5 );
157157
158- /** Compute the keypair for a secret key.
158+ /** Compute the keypair for a valid secret key.
159159 *
160- * Returns: 1: secret was valid, keypair is ready to use
161- * 0: secret was invalid, try again with a different secret
160+ * See the documentation of `secp256k1_ec_seckey_verify` for more information
161+ * about the validity of secret keys.
162+ *
163+ * Returns: 1: secret key is valid
164+ * 0: secret key is invalid
162165 * Args: ctx: pointer to a context object (not secp256k1_context_static).
163166 * Out: keypair: pointer to the created keypair.
164167 * In: seckey: pointer to a 32-byte secret key.
You can’t perform that action at this time.
0 commit comments