File tree 5 files changed +46
-51
lines changed
5 files changed +46
-51
lines changed Original file line number Diff line number Diff line change @@ -42,18 +42,16 @@ int main(void) {
42
42
assert (return_val );
43
43
44
44
/*** 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 (!fill_random (seckey1 , sizeof (seckey1 )) || !fill_random (seckey2 , sizeof (seckey2 ))) {
46
+ printf ("Failed to generate randomness\n" );
47
+ return 1 ;
48
+ }
49
+ /* 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. */
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 ;
57
55
}
58
56
59
57
/* 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) {
49
49
assert (return_val );
50
50
51
51
/*** 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 ;
64
62
}
65
63
66
64
/* 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) {
48
48
49
49
/*** Generate secret keys ***/
50
50
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 ;
62
61
}
63
62
64
63
/* 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) {
43
43
assert (return_val );
44
44
45
45
/*** 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 ;
60
58
}
61
59
62
60
/* Extract the X-only public key from the keypair. We pass NULL for
Original file line number Diff line number Diff line change @@ -679,12 +679,14 @@ SECP256K1_API int secp256k1_ecdsa_sign(
679
679
const void * ndata
680
680
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 );
681
681
682
- /** Verify an ECDSA secret key.
682
+ /** Verify an elliptic curve secret key.
683
683
*
684
684
* A secret key is valid if it is not 0 and less than the secp256k1 curve order
685
685
* when interpreted as an integer (most significant byte first). The
686
686
* probability of choosing a 32-byte string uniformly at random which is an
687
- * invalid secret key is negligible.
687
+ * invalid secret key is negligible. However, if it does happen it should
688
+ * be assumed that the randomness source is severely broken and there should
689
+ * be no retry.
688
690
*
689
691
* Returns: 1: secret key is valid
690
692
* 0: secret key is invalid
You can’t perform that action at this time.
0 commit comments