Skip to content

Commit ab49ce1

Browse files
committed
recovery: re-use secp256k1_ecdsa_sign_helper
The body of the recovery sign function is 99% the same secp256k1_ecdsa_sign. Now that this has moved to the sign helper, it can be re-used not only by the sign-to-contract module, but also by the recovery module.
1 parent fdd233a commit ab49ce1

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

src/modules/recovery/main_impl.h

+2-35
Original file line numberDiff line numberDiff line change
@@ -122,43 +122,10 @@ static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context *ctx, cons
122122

123123
int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecdsa_recoverable_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
124124
secp256k1_scalar r, s;
125-
secp256k1_scalar sec, non, msg;
125+
int ret;
126126
int recid;
127-
int ret = 0;
128-
int overflow = 0;
129-
VERIFY_CHECK(ctx != NULL);
130-
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
131-
ARG_CHECK(msg32 != NULL);
132127
ARG_CHECK(signature != NULL);
133-
ARG_CHECK(seckey != NULL);
134-
if (noncefp == NULL) {
135-
noncefp = secp256k1_nonce_function_default;
136-
}
137-
138-
secp256k1_scalar_set_b32(&sec, seckey, &overflow);
139-
/* Fail if the secret key is invalid. */
140-
if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
141-
unsigned char nonce32[32];
142-
unsigned int count = 0;
143-
secp256k1_scalar_set_b32(&msg, msg32, NULL);
144-
while (1) {
145-
ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
146-
if (!ret) {
147-
break;
148-
}
149-
secp256k1_scalar_set_b32(&non, nonce32, &overflow);
150-
if (!overflow && !secp256k1_scalar_is_zero(&non)) {
151-
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
152-
break;
153-
}
154-
}
155-
count++;
156-
}
157-
memset(nonce32, 0, 32);
158-
secp256k1_scalar_clear(&msg);
159-
secp256k1_scalar_clear(&non);
160-
secp256k1_scalar_clear(&sec);
161-
}
128+
ret = secp256k1_ecdsa_sign_helper(ctx, &r, &s, NULL, msg32, seckey, NULL, noncefp, noncedata, &recid);
162129
if (ret) {
163130
secp256k1_ecdsa_recoverable_signature_save(signature, &r, &s, recid);
164131
} else {

src/secp256k1.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,16 @@ static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_s
519519
}
520520

521521
int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) {
522-
return secp256k1_ecdsa_sign_helper(ctx, signature, NULL, msg32, seckey, NULL, noncefp, noncedata);
522+
secp256k1_scalar r, s;
523+
int ret;
524+
ARG_CHECK(signature != NULL);
525+
ret = secp256k1_ecdsa_sign_helper(ctx, &r, &s, NULL, msg32, seckey, NULL, noncefp, noncedata, NULL);
526+
if (ret) {
527+
secp256k1_ecdsa_signature_save(signature, &r, &s);
528+
} else {
529+
memset(signature, 0, sizeof(*signature));
530+
}
531+
return ret;
523532
}
524533

525534
int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {

0 commit comments

Comments
 (0)