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

Add on-by-default fast crate feature for gating basepoint tables #251

Merged
merged 5 commits into from
Jan 20, 2023

Conversation

tarcieri
Copy link
Contributor

@tarcieri tarcieri commented Dec 28, 2022

NOTE: depends on dalek-cryptography/curve25519-dalek#489 which needs to be merged first Merged!

NOTE: now depends on #260 which needs to be merged first Merged!

Disabling the feature reduces overall code size at the cost of performance, which is useful for e.g. embedded users.

This feature transitively enables the basepoint-tables feature in curve25519-dalek.

TODO: fallback support for using variable-base scalar multiplication by the basepoint when the basepoint-tables feature is disabled. Added in 8f56c5c

.github/workflows/rust.yml Outdated Show resolved Hide resolved
@tarcieri
Copy link
Contributor Author

tarcieri commented Dec 28, 2022

It might make more sense to give this feature a simpler name like fast which makes its function and code-size-vs-performance tradeoff more readily apparent to non-expert end-users.

Embedded users who start with --no-default-features can experiment with turning it back on and seeing if the larger code size blows up their program (something I've seen happen in person with M0/M4 users trying to use ed25519-dalek at OxidizeConf's hack days)

Edit: went ahead and renamed the feature to fast

@tarcieri tarcieri changed the title [WIP] Add on-by-default basepoint-tables crate feature [WIP] Add on-by-default fast crate feature for gating basepoint tables Dec 29, 2022
src/mul_base.rs Outdated Show resolved Hide resolved
@pinkforest pinkforest mentioned this pull request Jan 5, 2023
@tarcieri tarcieri force-pushed the feature-gate-basepoint-tables branch 2 times, most recently from d00d251 to 0d9253e Compare January 8, 2023 18:26
@tarcieri tarcieri changed the title [WIP] Add on-by-default fast crate feature for gating basepoint tables Add on-by-default fast crate feature for gating basepoint tables Jan 8, 2023
@tarcieri tarcieri force-pushed the feature-gate-basepoint-tables branch from 0d9253e to 2c5df02 Compare January 8, 2023 18:29
@tarcieri
Copy link
Contributor Author

tarcieri commented Jan 8, 2023

Not sure why the build is failing. I split out #260 to handle updating to the latest upstream curve25519-dalek changes as an intermediate debugging step.

@tarcieri tarcieri force-pushed the feature-gate-basepoint-tables branch 2 times, most recently from a067342 to c807eb1 Compare January 9, 2023 14:47
@tarcieri tarcieri marked this pull request as ready for review January 9, 2023 14:55
@tarcieri
Copy link
Contributor Author

tarcieri commented Jan 9, 2023

@rozbb this is ready now

@tarcieri tarcieri force-pushed the feature-gate-basepoint-tables branch 2 times, most recently from 557dbda to 05cced0 Compare January 15, 2023 04:03
Disabling the feature reduces overall code size at the cost of
performance, which is useful for e.g. embedded users.

This feature transitively enables the `basepoint-tables` feature in
`curve25519-dalek` where the basepoint tables are actually defined.
@tarcieri tarcieri force-pushed the feature-gate-basepoint-tables branch from 05cced0 to 65a936a Compare January 15, 2023 04:06
@tarcieri tarcieri requested a review from rozbb January 15, 2023 04:11
@rozbb
Copy link
Contributor

rozbb commented Jan 16, 2023

Just ran some benchmarks on M1 and x86_64. It looks like we get massive (64%) speedups in keygen and signing. But we get a 0-4% slowdown in batch verification, and no effect at all for single verification. Do you wanna rename to fast-sign?

@tarcieri
Copy link
Contributor Author

Haha what? That's surprising.

If that's really the case then batch verification should always use ... * ED25519_BASEPOINT_POINT if using mul_base has a negative impact on performance when the basepoint tables feature is enabled

@rozbb
Copy link
Contributor

rozbb commented Jan 16, 2023

Yeah makes sense. First I'll test how the feature interacts with the simd backends.

Update: yup, same slowdown even with AVX2 SIMD

@rozbb
Copy link
Contributor

rozbb commented Jan 16, 2023

Yup same slowdown even with AVX2 SIMD. And actually, single signature verification is 5-9% slower with fast enabled. So there's no situation wherein verification benefits from the tables. This is really surprising.

I'll do the following: rename the feature to fast-sign, explain it in the README (maybe after merging the other README PR), and make all verification use table-less scalar mult.

@tarcieri
Copy link
Contributor Author

I still think we should remove mul_base anywhere it isn’t helping and keep the feature name as fast.

If there are slowdowns occurring in places where mul_base isn’t used, that is a real mystery

@tarcieri
Copy link
Contributor Author

tarcieri commented Jan 17, 2023

Perhaps we should open an issue on curve25519-dalek?

Tracing the code I'm perplexed how this feature would have any effect on verification, either single signature or batch verification, as as far as I can tell neither use the basepoint tables? (although there are many backends and maybe I haven't grepped/traced properly)

Batch verification calls EdwardsPoint::optional_multiscalar_mul which starts with the basepoint constant (i.e. point, not table). That calls either Straus or Pippinger backends for multiscalar multiplication, which receive all their scalar/point inputs explicitly regardless of if one of them is the basepoint.

Single signature verification calls EdwardsPoint::vartime_double_scalar_mul_basepoint which calls scalar_mul::vartime_double_base::mul which uses a completely different basepoint table AFFINE_ODD_MULTIPLES_OF_BASEPOINT located in backend::serial::u64::constants (which, sidebar, should probably also be gated by the precomputed tables feature, whatever we end up calling it)

@rozbb
Copy link
Contributor

rozbb commented Jan 17, 2023

Yeah I ran into the same thing when tracing this down. I have no idea what's causing this. I'll spend a little more time tracing things

context: Option<&[u8]>,
signature: &InternalSignature,
M: &[u8],
) -> EdwardsPoint {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: this will likely return a CompressedEdwardsY in the process of fixing #267 . Documenting the comment here:

Note that this returns the compressed form of R and the caller does a byte comparison.
This means that all our verification functions do not accept non-canonically encoded R values.
See the validation criteria blog post for more details:
    https://hdevalence.ca/blog/2020-10-04-its-25519am

@tarcieri
Copy link
Contributor Author

@rozbb let me update curve25519-dalek in this PR now that the feature name has changed to precomputed-tables

@tarcieri
Copy link
Contributor Author

Updated and it's green again

@rozbb rozbb merged commit f61e9dc into release/2.0 Jan 20, 2023
@tarcieri tarcieri deleted the feature-gate-basepoint-tables branch January 20, 2023 20:47
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.

2 participants