Skip to content

Upstream PR 1058#308

Closed
mllwchrry wants to merge 17 commits intoBlockstreamResearch:masterfrom
mllwchrry:temp-merge-1058
Closed

Upstream PR 1058#308
mllwchrry wants to merge 17 commits intoBlockstreamResearch:masterfrom
mllwchrry:temp-merge-1058

Conversation

@mllwchrry
Copy link
Contributor

Merge bitcoin-core/secp256k1#1058: Signed-digit multi-comb ecmult_gen algorithm

This PR can be recreated with ./contrib/sync-upstream.sh -b master range da51507.
Tip: Use git show --remerge-diff to show the changes manually added to the merge commit.

real-or-random and others added 17 commits January 30, 2026 14:00
Instead of having the starting point of the ecmult_gen computation be
offset, do it with the final point. This enables reasoning over the
set of points reachable in intermediary computations, which can be
leveraged by potential future optimization.

Because the final point is in affine coordinates, its projective
blinding is no longer possible. It will be reintroduced again in
a different way, in a later commit.

Also introduce some more comments and more descriptive names.
The old code overwrote the input at the start of the function,
making a call like secp256k1_scalar_inverse(&x,&x) always fail.
This introduces the signed-digit multi-comb multiplication algorithm
for constant-time G multiplications (ecmult_gen). It is based on
section 3.3 of "Fast and compact elliptic-curve cryptography" by
Mike Hamburg (see https://eprint.iacr.org/2012/309).

Original implementation by Peter Dettman, with changes by Pieter Wuille
to use scalars for recoding, and additional comments.
It is unnecessary to recompute this term needed by the SDMC algorithm
for every multiplication; move it into the context scalar_offset value
instead.
The old code would trigger UB when count=32.
The existing code needs to deal with the edge case that bit_pos >= 256,
which would lead to an out-of-bounds read from secp256k1_scalar.

Instead, recode the scalar into an array of uint32_t with enough zero
padding at the end to alleviate the issue. This also simplifies the
code, and is necessary for a security improvement in a follow-up
commit.

Original code by Peter Dettman, with modifications by Pieter Wuille.
Co-authored-by: Tim Ruffing <crypto@timruffing.de>
Copy link
Contributor

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! In general, your PR looks good but it seems like rebasing on the previous sync PR (#307) removed the upstream merge commit (da51507) from the branch. Since I had already used sync-upstream.sh to review this PR, I created alternative PR #310.

Comment on lines +54 to +55

SECP256K1_SCALAR_VERIFY(r);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is unrelated to #1058. Can you do that in a separate PR?

Comment on lines +76 to +77

SECP256K1_SCALAR_VERIFY(r);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is unrelated to #1058. Can you do that in a separate PR?

*r = v % EXHAUSTIVE_TEST_ORDER;

secp256k1_scalar_verify(r);
SECP256K1_SCALAR_VERIFY(r);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is unrelated to #1058. Can you do that in a separate PR?

Comment on lines 670 to 674
echo " ecmult window size = $set_ecmult_window"
echo " ecmult gen prec. bits = $set_ecmult_gen_precision"
echo " ecmult gen table size = $set_ecmult_gen_kb KiB"
# Hide test-only options unless they're used.
if test x"$set_widemul" != xauto; then
echo " wide multiplication = $set_widemul"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation

@jonasnick jonasnick closed this Jan 30, 2026
@mllwchrry mllwchrry deleted the temp-merge-1058 branch February 9, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants