-
Notifications
You must be signed in to change notification settings - Fork 586
Runtime backend autodetection #523
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
Changes from all commits
91e839a
0db8783
219995d
1b6fee3
996b1e9
738cfee
a7df9c7
c67e430
94247a7
5028971
50aa635
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -53,6 +53,9 @@ curve25519-dalek = "4.0.0-rc.2" | |||||||||||||
| | `alloc` | ✓ | Enables Edwards and Ristretto multiscalar multiplication, batch scalar inversion, and batch Ristretto double-and-compress. Also enables `zeroize`. | | ||||||||||||||
| | `zeroize` | ✓ | Enables [`Zeroize`][zeroize-trait] for all scalar and curve point types. | | ||||||||||||||
| | `precomputed-tables` | ✓ | Includes precomputed basepoint multiplication tables. This speeds up `EdwardsPoint::mul_base` and `RistrettoPoint::mul_base` by ~4x, at the cost of ~30KB added to the code size. | | ||||||||||||||
| | `simd_avx2` | ✓ | Allows the AVX2 SIMD backend to be used, if available. | | ||||||||||||||
| | `simd_avx512` | ✓ | Allows the AVX512 SIMD backend to be used, if available. | | ||||||||||||||
| | `simd` | ✓ | Allows every SIMD backend to be used, if available. | | ||||||||||||||
|
Comment on lines
+56
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this:
Suggested change
Optionally could make the forbid as cfg as is done with the rest of the configuration. |
||||||||||||||
| | `rand_core` | | Enables `Scalar::random` and `RistrettoPoint::random`. This is an optional dependency whose version is not subject to SemVer. See [below](#public-api-semver-exemptions) for more details. | | ||||||||||||||
| | `digest` | | Enables `RistrettoPoint::{from_hash, hash_from_bytes}` and `Scalar::{from_hash, hash_from_bytes}`. This is an optional dependency whose version is not subject to SemVer. See [below](#public-api-semver-exemptions) for more details. | | ||||||||||||||
| | `serde` | | Enables `serde` serialization/deserialization for all the point and scalar types. | | ||||||||||||||
|
|
@@ -95,30 +98,26 @@ See tracking issue: [curve25519-dalek/issues/521](https://github.com/dalek-crypt | |||||||||||||
|
|
||||||||||||||
| Curve arithmetic is implemented and used by selecting one of the following backends: | ||||||||||||||
|
|
||||||||||||||
| | Backend | Implementation | Target backends | | ||||||||||||||
| | :--- | :--- | :--- | | ||||||||||||||
| | `[default]` | Serial formulas | `u32` <br/> `u64` | | ||||||||||||||
| | `simd` | [Parallel][parallel_doc], using Advanced Vector Extensions | `avx2` <br/> `avx512ifma` | | ||||||||||||||
| | `fiat` | Formally verified field arithmetic from [fiat-crypto] | `fiat_u32` <br/> `fiat_u64` | | ||||||||||||||
| | Backend | Implementation | Target backends | | ||||||||||||||
| | :--- | :--- | :--- | | ||||||||||||||
| | `[default]` | Automatic runtime backend selection (either serial or SIMD) | `u32` <br/> `u64` <br/> `avx2` <br/> `avx512` | | ||||||||||||||
| | `fiat` | Formally verified field arithmetic from [fiat-crypto] | `fiat_u32` <br/> `fiat_u64` | | ||||||||||||||
|
|
||||||||||||||
| To choose a backend other than the `[default]` serial backend, set the | ||||||||||||||
| To choose a backend other than the `[default]` backend, set the | ||||||||||||||
| environment variable: | ||||||||||||||
| ```sh | ||||||||||||||
| RUSTFLAGS='--cfg curve25519_dalek_backend="BACKEND"' | ||||||||||||||
| ``` | ||||||||||||||
| where `BACKEND` is `simd` or `fiat`. Equivalently, you can write to | ||||||||||||||
| where `BACKEND` is `fiat`. Equivalently, you can write to | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, if |
||||||||||||||
| `~/.cargo/config`: | ||||||||||||||
| ```toml | ||||||||||||||
| [build] | ||||||||||||||
| rustflags = ['--cfg=curve25519_dalek_backend="BACKEND"'] | ||||||||||||||
| ``` | ||||||||||||||
| More info [here](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags). | ||||||||||||||
|
|
||||||||||||||
| The `simd` backend requires extra configuration. See [the SIMD | ||||||||||||||
| section](#simd-target-backends). | ||||||||||||||
|
|
||||||||||||||
| Note for contributors: The target backends are not entirely independent of each | ||||||||||||||
| other. The `simd` backend directly depends on parts of the the `u64` backend to | ||||||||||||||
| other. The SIMD backend directly depends on parts of the the `u64` backend to | ||||||||||||||
| function. | ||||||||||||||
|
|
||||||||||||||
| ## Word size for serial backends | ||||||||||||||
|
|
@@ -137,7 +136,7 @@ RUSTFLAGS='--cfg curve25519_dalek_bits="SIZE"' | |||||||||||||
| where `SIZE` is `32` or `64`. As in the above section, this can also be placed | ||||||||||||||
| in `~/.cargo/config`. | ||||||||||||||
|
|
||||||||||||||
| **NOTE:** The `simd` backend CANNOT be used with word size 32. | ||||||||||||||
| **NOTE:** Using a word size of 32 will automatically disable SIMD support. | ||||||||||||||
|
|
||||||||||||||
| ### Cross-compilation | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -152,18 +151,19 @@ $ cargo build --target i686-unknown-linux-gnu | |||||||||||||
|
|
||||||||||||||
| ## SIMD target backends | ||||||||||||||
|
|
||||||||||||||
| Target backend selection within `simd` must be done manually by setting the | ||||||||||||||
| `RUSTFLAGS` environment variable to one of the below options: | ||||||||||||||
| The SIMD target backend selection is done automatically at runtime depending | ||||||||||||||
| on the available CPU features, provided the appropriate feature flag is enabled. | ||||||||||||||
|
|
||||||||||||||
| | CPU feature | `RUSTFLAGS` | Requires nightly? | | ||||||||||||||
| | :--- | :--- | :--- | | ||||||||||||||
| | avx2 | `-C target_feature=+avx2` | no | | ||||||||||||||
| | avx512ifma | `-C target_feature=+avx512ifma` | yes | | ||||||||||||||
|
Comment on lines
-158
to
-161
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed all of this documentation is removed, but the newly introduced crate features aren't equivalent and perhaps this should still be documented. Namely these flags bypass autodetection and force the use of SIMD features.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I can take care of documenting that - just need to re-organise a bit to make room for the vendored dep first |
||||||||||||||
| You can also specify an appropriate `-C target_feature` to build a binary | ||||||||||||||
| which assumes the required SIMD instructions are always available. | ||||||||||||||
|
|
||||||||||||||
| Or you can use `-C target_cpu=native` if you don't know what to set. | ||||||||||||||
| | Backend | Feature flag | `RUSTFLAGS` | Requires nightly? | | ||||||||||||||
| | :--- | :--- | :--- | :--- | | ||||||||||||||
| | avx2 | `simd_avx2` | `-C target_feature=+avx2` | no | | ||||||||||||||
| | avx512 | `simd_avx512` | `-C target_feature=+avx512ifma,+avx512vl` | yes | | ||||||||||||||
|
|
||||||||||||||
| The AVX512 backend requires Rust nightly. If enabled and when compiled on a non-nightly | ||||||||||||||
| compiler it will fall back to using the AVX2 backend. | ||||||||||||||
| The AVX512 backend requires Rust nightly. When compiled on a non-nightly | ||||||||||||||
| compiler it will always be disabled. | ||||||||||||||
|
|
||||||||||||||
| # Documentation | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -243,16 +243,16 @@ The implementation is memory-safe, and contains no significant | |||||||||||||
| `unsafe` code. The SIMD backend uses `unsafe` internally to call SIMD | ||||||||||||||
| intrinsics. These are marked `unsafe` only because invoking them on an | ||||||||||||||
| inappropriate CPU would cause `SIGILL`, but the entire backend is only | ||||||||||||||
| compiled with appropriate `target_feature`s, so this cannot occur. | ||||||||||||||
| invoked when the appropriate CPU features are detected at runtime, or | ||||||||||||||
| when the whole program is compiled with the appropriate `target_feature`s. | ||||||||||||||
|
|
||||||||||||||
| # Performance | ||||||||||||||
|
|
||||||||||||||
| Benchmarks are run using [`criterion.rs`][criterion]: | ||||||||||||||
|
|
||||||||||||||
| ```sh | ||||||||||||||
| cargo bench --features "rand_core" | ||||||||||||||
| # Uses avx2 or ifma only if compiled for an appropriate target. | ||||||||||||||
| export RUSTFLAGS='--cfg curve25519_dalek_backend="simd" -C target_cpu=native' | ||||||||||||||
| export RUSTFLAGS='-C target_cpu=native' | ||||||||||||||
| cargo +nightly bench --features "rand_core" | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -294,7 +294,7 @@ universe's beauty, but also his deep hatred of the Daleks. Rusty destroys the | |||||||||||||
| other Daleks and departs the ship, determined to track down and bring an end | ||||||||||||||
| to the Dalek race.* | ||||||||||||||
|
|
||||||||||||||
| `curve25519-dalek` is authored by Isis Agora Lovecruft and Henry de Valence. | ||||||||||||||
| `curve25519-dalek` is authored by Isis Agora Lovecruft and Henry de Valence. | ||||||||||||||
|
|
||||||||||||||
| Portions of this library were originally a port of [Adam Langley's | ||||||||||||||
| Golang ed25519 library](https://github.com/agl/ed25519), which was in | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.