Closed
Conversation
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>
f597eaf to
c575071
Compare
jonasnick
reviewed
Jan 30, 2026
Comment on lines
+54
to
+55
|
|
||
| SECP256K1_SCALAR_VERIFY(r); |
Contributor
There was a problem hiding this comment.
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); |
Contributor
There was a problem hiding this comment.
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); |
Contributor
There was a problem hiding this comment.
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-diffto show the changes manually added to the merge commit.