Skip to content

Commit 93067b1

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 baec472 commit 93067b1

File tree

3 files changed

+26
-47
lines changed

3 files changed

+26
-47
lines changed

src/modules/ecdsa_sign_to_contract/main_impl.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
#include "include/secp256k1_ecdsa_sign_to_contract.h"
1111

1212
int secp256k1_ecdsa_s2c_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32) {
13-
return secp256k1_ecdsa_sign_helper(ctx, signature, s2c_opening, msg32, seckey, s2c_data32, NULL, NULL);
13+
secp256k1_scalar r, s;
14+
int ret;
15+
ARG_CHECK(signature != NULL);
16+
ret = secp256k1_ecdsa_sign_helper(ctx, &r, &s, s2c_opening, msg32, seckey, s2c_data32, NULL, NULL, NULL);
17+
if (ret) {
18+
secp256k1_ecdsa_signature_save(signature, &r, &s);
19+
} else {
20+
memset(signature, 0, sizeof(*signature));
21+
}
22+
return ret;
1423
}
1524

1625
int secp256k1_ecdsa_s2c_verify_commit(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *data32, const secp256k1_s2c_opening *opening) {

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,19 @@ const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function
447447
const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979;
448448

449449
/* TODO: re-order functions in this file so forward declarations are not needed? */
450-
static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32, secp256k1_nonce_function noncefp, const void* noncedata);
450+
static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32, secp256k1_nonce_function noncefp, const void* noncedata, int *recid);
451451

452452
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) {
453-
return secp256k1_ecdsa_sign_helper(ctx, signature, NULL, msg32, seckey, NULL, noncefp, noncedata);
453+
secp256k1_scalar r, s;
454+
int ret;
455+
ARG_CHECK(signature != NULL);
456+
ret = secp256k1_ecdsa_sign_helper(ctx, &r, &s, NULL, msg32, seckey, NULL, noncefp, noncedata, NULL);
457+
if (ret) {
458+
secp256k1_ecdsa_signature_save(signature, &r, &s);
459+
} else {
460+
memset(signature, 0, sizeof(*signature));
461+
}
462+
return ret;
454463
}
455464

456465
int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) {
@@ -758,16 +767,15 @@ int secp256k1_s2c_opening_serialize(const secp256k1_context* ctx, unsigned char
758767
return secp256k1_ec_pubkey_serialize(ctx, &output34[1], &outputlen, &opening->original_pubnonce, SECP256K1_EC_COMPRESSED);
759768
}
760769

761-
static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32, secp256k1_nonce_function noncefp, const void* noncedata) {
762-
secp256k1_scalar r, s;
770+
static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, secp256k1_s2c_opening *s2c_opening, const unsigned char *msg32, const unsigned char *seckey, const unsigned char* s2c_data32, secp256k1_nonce_function noncefp, const void* noncedata, int *recid) {
763771
secp256k1_scalar sec, non, msg;
764772
int ret = 0;
765773
int overflow = 0;
766774
unsigned char ndata[32];
767775
VERIFY_CHECK(ctx != NULL);
768776
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
769777
ARG_CHECK(msg32 != NULL);
770-
ARG_CHECK(signature != NULL);
778+
ARG_CHECK(r != NULL && s != NULL);
771779
ARG_CHECK(seckey != NULL);
772780
if (noncefp == NULL) {
773781
noncefp = secp256k1_nonce_function_default;
@@ -829,7 +837,7 @@ static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_e
829837
}
830838

831839
if (!overflow && !is_zero) {
832-
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
840+
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid)) {
833841
break;
834842
}
835843
}
@@ -841,11 +849,6 @@ static int secp256k1_ecdsa_sign_helper(const secp256k1_context *ctx, secp256k1_e
841849
secp256k1_scalar_clear(&non);
842850
secp256k1_scalar_clear(&sec);
843851
}
844-
if (ret) {
845-
secp256k1_ecdsa_signature_save(signature, &r, &s);
846-
} else {
847-
memset(signature, 0, sizeof(*signature));
848-
}
849852
return ret;
850853
}
851854

0 commit comments

Comments
 (0)