Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signed-digit multi-comb ecmult_gen algorithm #1057

Closed
wants to merge 12 commits into from

Conversation

sipa
Copy link
Contributor

@sipa sipa commented Dec 27, 2021

A third iteration of the signed-digit multi-comb ecmult_gen algorithm (earlier attempts: #693, and #546 by Peter Dettman). Short summary:

  • A new constant-time point multiplication algorithm with precomputation (so only used for multiply with G).
  • Based on section 3.3 of https://eprint.iacr.org/2012/309 by Mike Hamburg.
  • Configurable through two parameters: COMB_BLOCKS and COMB_TEETH
    • Currently only 3 predefined configurations reachable through ./configure (tables 2 kB, 22 kB, 86 kB). All three are included in precomputed_ecmult_gen.c and tested in CI. The 2 kB option is already comparable in speed with the current code.
    • Many more configurations can be reached by manually setting the macros. These are not tested.

Compared with the previous PR #693:

  • Updated to the new static-precomputation-only model (Fully static precomputation tables #893).
  • Just 3 curated configurations reachable through configure.
  • Removed some optimizations that do not matter (much).
  • Do blinding through an final correction add rather than an initial start point, which may later permit usage of incomplete addition formulae (Try a non-uniform group law (e.g., for ecmult_gen)? #1051).
  • The recoding of the input scalar to signed bit representation is done slightly differently, which needs fewer special cases.

@sipa sipa changed the title WIP Reword of Signed-Digit Multicomb WIP Rework of Signed-Digit Multicomb Dec 27, 2021
@peterdettman
Copy link
Contributor

Just out of curiosity, some perf. numbers (best Min of 3 bench sign, 64-bit, i7-9750H):

branch ecdsa_sign schnorrsig_sign
master 29.5 23.1
this PR 25.6 19.2
experimental 23.8 17.7

("experimental" is this PR plus the other PRs for normalize, group formulae, and the "vector" modinv).

sipa added 2 commits December 28, 2021 19:03
This introduces a new secp256k1_scalar_half function which multiplies
a scalar with the multiplicative inverse of 2 (modulo order).
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.
@sipa
Copy link
Contributor Author

sipa commented Dec 29, 2021

@peterdettman Care to redo benchmarks for the latest commit (I've removed the incomplete comb optimization, and re-added the uint32_t[9] recoded approach)?

@siv2r
Copy link
Contributor

siv2r commented Dec 29, 2021

These were the best min I got (running the benchmark thrice) on my machine (64-bit, i7-8750H).

branch ecdsa_sign schnorrsig_sign
master 64.4 49.6
this PR 57.0 42.0

@peterdettman
Copy link
Contributor

peterdettman commented Dec 29, 2021

Updated perf. numbers (-O3, best Min of 3 bench sign, 64-bit, i7-9750H):

branch ecdsa_sign schnorrsig_sign
master 29.6 23.1
this PR 25.7 19.4
experimental 24.2 17.9

("experimental" is this PR plus the other PRs for normalize, group formulae, and the "vector" modinv).

So this looks just slightly slower than before, but perfectly fine if we are merge-focused. We can go hunting the extra 2% once we've booked the 20%.

@sipa sipa changed the title WIP Rework of Signed-Digit Multicomb Signed-digit multi-comb ecmult_gen algorithm Dec 29, 2021
peterdettman and others added 10 commits December 29, 2021 15:54
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 the 2^COMB_BITS-1 scalar offset needed
by the SDMC algorithm for every multiplication; move it into the
context scalar_offset value instead.
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.
@sipa
Copy link
Contributor Author

sipa commented Dec 29, 2021

Restarting this in a new PR to avoid the WIP discussion: #1058.

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.

3 participants