Skip to content

Commit cd45ae7

Browse files
jonasnickdeadalnix
authored andcommitted
Separate helper function for ec_pubkey_tweak_add
Summary: This is in preparation for allowing code reuse by xonly tweak add functions This is a partial backport of secp256k1 [[bitcoin-core/secp256k1#558 | PR558]] : bitcoin-core/secp256k1@176bfb1 Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D7640
1 parent e5ffbae commit cd45ae7

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/secp256k1.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,25 +632,26 @@ int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *
632632
return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
633633
}
634634

635+
static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak) {
636+
secp256k1_scalar term;
637+
int overflow = 0;
638+
secp256k1_scalar_set_b32(&term, tweak, &overflow);
639+
return !overflow && secp256k1_eckey_pubkey_tweak_add(ecmult_ctx, p, &term);
640+
}
641+
635642
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
636643
secp256k1_ge p;
637-
secp256k1_scalar term;
638644
int ret = 0;
639-
int overflow = 0;
640645
VERIFY_CHECK(ctx != NULL);
641646
ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
642647
ARG_CHECK(pubkey != NULL);
643648
ARG_CHECK(tweak != NULL);
644649

645-
secp256k1_scalar_set_b32(&term, tweak, &overflow);
646-
ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
650+
ret = secp256k1_pubkey_load(ctx, &p, pubkey);
647651
memset(pubkey, 0, sizeof(*pubkey));
652+
ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &p, tweak);
648653
if (ret) {
649-
if (secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term)) {
650-
secp256k1_pubkey_save(pubkey, &p);
651-
} else {
652-
ret = 0;
653-
}
654+
secp256k1_pubkey_save(pubkey, &p);
654655
}
655656

656657
return ret;

0 commit comments

Comments
 (0)