2016-12-08 05:12:00 +00:00
2022-12-09 08:19:53 +00:00
# curve25519-dalek [](https://crates.io/crates/curve25519-dalek) [](https://docs.rs/curve25519-dalek) [](https://github.com/dalek-cryptography/curve25519-dalek/actions/workflows/rust.yml)
2016-12-08 05:12:00 +00:00
2022-12-09 08:19:53 +00:00
< p align = "center" >
2018-01-19 23:01:47 +00:00
< img
2022-11-26 19:11:57 +00:00
alt="dalek-cryptography logo: a dalek with edwards curves as sparkles coming out of its radar-schnozzley blaster thingies"
2022-12-09 08:19:53 +00:00
width="200px"
2022-12-07 10:36:07 +00:00
src="https://cdn.jsdelivr.net/gh/dalek-cryptography/curve25519-dalek/docs/assets/dalek-logo-clear.png"/>
2022-11-26 19:11:57 +00:00
< / p >
2018-01-19 23:01:47 +00:00
2018-01-25 21:40:30 +00:00
**A pure-Rust implementation of group operations on Ristretto and Curve25519.**
2016-12-08 05:12:00 +00:00
2018-01-25 21:40:30 +00:00
`curve25519-dalek` is a library providing group operations on the Edwards and
Montgomery forms of Curve25519, and on the prime-order Ristretto group.
2016-12-08 05:12:00 +00:00
2018-01-25 21:40:30 +00:00
`curve25519-dalek` is not intended to provide implementations of any particular
crypto protocol. Rather, implementations of those protocols (such as
[`x25519-dalek`][x25519-dalek] and [`ed25519-dalek`][ed25519-dalek]) should use
`curve25519-dalek` as a library.
2016-12-08 05:12:00 +00:00
2018-01-25 21:40:30 +00:00
`curve25519-dalek` is intended to provide a clean and safe _mid-level_ API for use
implementing a wide range of ECC-based crypto protocols, such as key agreement,
signatures, anonymous credentials, rangeproofs, and zero-knowledge proof
systems.
2016-12-08 22:18:57 +00:00
2018-01-26 01:34:06 +00:00
In particular, `curve25519-dalek` implements Ristretto, which constructs a
prime-order group from a non-prime-order Edwards curve. This provides the
speed and safety benefits of Edwards curve arithmetic, without the pitfalls of
cofactor-related abstraction mismatches.
2018-01-25 21:40:30 +00:00
# Use
2016-12-08 22:18:57 +00:00
2022-12-12 07:54:25 +00:00
## Stable
2018-01-25 21:40:30 +00:00
To import `curve25519-dalek` , add the following to the dependencies section of
your project's `Cargo.toml` :
2017-12-02 03:19:19 +00:00
```toml
2022-12-12 07:54:25 +00:00
curve25519-dalek = "3"
```
## Beta
To use the latest prerelease (see changes [below ](#breaking-changes-in-400 )),
use the following line in your project's `Cargo.toml` :
```toml
2023-03-26 06:49:20 +00:00
curve25519-dalek = "4.0.0-rc.2"
2017-12-02 03:19:19 +00:00
```
2016-12-08 22:18:57 +00:00
2022-12-09 08:19:53 +00:00
## Feature Flags
2023-01-08 08:51:51 +00:00
| Feature | Default? | Description |
| :--- | :---: | :--- |
| `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. |
2023-01-19 19:04:22 +00:00
| `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. |
2023-01-08 08:51:51 +00:00
| `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. |
2022-12-09 08:19:53 +00:00
To disable the default features when using `curve25519-dalek` as a dependency,
add `default-features = false` to the dependency in your `Cargo.toml` . To
disable it when running `cargo` , add the `--no-default-features` CLI flag.
2021-04-14 02:28:21 +00:00
## Major Version API Changes
2022-12-09 08:19:53 +00:00
Breaking changes for each major version release can be found in
[`CHANGELOG.md` ](CHANGELOG.md ), under the "Breaking changes" subheader. The
2023-03-26 06:49:20 +00:00
latest breaking changes in high level are below:
2021-04-14 02:28:21 +00:00
2022-12-09 08:19:53 +00:00
### Breaking changes in 4.0.0
2021-04-14 02:28:21 +00:00
2022-12-27 10:12:55 +00:00
* Update the MSRV from 1.41 to 1.60
2023-03-26 06:49:20 +00:00
* Provide SemVer policy
* Make `digest` and `rand_core` optional features
* Remove `std` and `nightly` features
* Replace backend selection - See [CHANGELOG.md ](CHANGELOG.md ) and [backends ](#backends )
2022-12-12 07:54:25 +00:00
* Replace methods `Scalar::{zero, one}` with constants `Scalar::{ZERO, ONE}`
2022-12-12 23:03:58 +00:00
* `Scalar::from_canonical_bytes` now returns `CtOption`
* `Scalar::is_canonical` now returns `Choice`
2022-12-09 08:19:53 +00:00
* Deprecate `EdwardsPoint::hash_from_bytes` and rename it
`EdwardsPoint::nonspec_map_to_curve`
* Require including a new trait, `use curve25519_dalek::traits::BasepointTable`
whenever using `EdwardsBasepointTable` or `RistrettoBasepointTable`
2021-04-14 02:28:21 +00:00
2022-12-09 08:19:53 +00:00
This release also does a lot of dependency updates and relaxations to unblock upstream build issues.
2021-04-14 02:28:21 +00:00
2023-03-26 06:49:20 +00:00
### 4.0.0 - Open Breaking Changes
See tracking issue: [curve25519-dalek/issues/521 ](https://github.com/dalek-cryptography/curve25519-dalek/issues/521 )
2022-12-09 08:19:53 +00:00
# Backends
2021-04-14 02:28:21 +00:00
2022-12-11 21:37:50 +00:00
Curve arithmetic is implemented and used by selecting one of the following backends:
2021-04-14 01:30:57 +00:00
2022-12-11 21:37:50 +00:00
| Backend | Implementation | Target backends |
2022-12-09 08:19:53 +00:00
| :--- | :--- | :--- |
2022-12-11 21:37:50 +00:00
| `[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` |
2020-08-18 01:20:26 +00:00
2022-12-11 21:37:50 +00:00
To choose a backend other than the `[default]` serial backend, set the
environment variable:
```sh
RUSTFLAGS='--cfg curve25519_dalek_backend="BACKEND"'
```
where `BACKEND` is `simd` or `fiat` . Equivalently, you can write to
`~/.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
function.
## Word size for serial backends
2019-10-25 23:03:31 +00:00
2022-12-11 21:37:50 +00:00
`curve25519-dalek` will automatically choose the word size for the `[default]`
and `fiat` serial backends, based on the build target. For example, building
for a 64-bit machine, the default `u64` target backend is automatically chosen
when the `[default]` backend is selected, and `fiat_u64` is chosen when the
`fiat backend is selected.
2022-11-13 17:17:42 +00:00
2022-12-11 21:37:50 +00:00
Backend word size can be overridden for `[default]` and `fiat` by setting the
environment variable:
```sh
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.
### Cross-compilation
Because backend selection is done by target, cross-compiling will select the
correct word size automatically. For example, on an x86-64 Linux machine,
`curve25519-dalek` will use the `u32` target backend if the following is run:
```console
$ sudo apt install gcc-multilib # (or whatever package manager you use)
$ rustup target add i686-unknown-linux-gnu
$ cargo build --target i686-unknown-linux-gnu
```
2022-11-13 17:17:42 +00:00
2022-12-11 21:37:50 +00:00
## SIMD target backends
2019-10-25 23:03:31 +00:00
2022-12-11 21:37:50 +00:00
Target backend selection within `simd` must be done manually by setting the
`RUSTFLAGS` environment variable to one of the below options:
2017-12-02 03:19:19 +00:00
2022-12-11 21:37:50 +00:00
| CPU feature | `RUSTFLAGS` |
| :--- | :--- |
| avx2 | `-C target_feature=+avx2` |
| avx512ifma | `-C target_feature=+avx512ifma` |
2018-05-15 00:35:34 +00:00
2022-12-11 21:37:50 +00:00
Or you can use `-C target_cpu=native` if you don't know what to set.
2016-12-08 22:18:57 +00:00
2022-12-11 21:37:50 +00:00
The `simd` backend also requires using nightly, e.g. by running `cargo
+nightly build`, to build.
2018-05-15 00:35:34 +00:00
2022-12-09 08:19:53 +00:00
# Documentation
2018-07-05 20:39:29 +00:00
2022-12-09 08:19:53 +00:00
The semver-stable, public-facing `curve25519-dalek` API is documented [here][docs].
2022-11-13 17:17:42 +00:00
2022-12-09 08:19:53 +00:00
## Building Docs Locally
2022-11-13 17:17:42 +00:00
2022-12-09 08:19:53 +00:00
The `curve25519-dalek` documentation requires a custom HTML header to include
KaTeX for math support. Unfortunately `cargo doc` does not currently support
this, but docs can be built using
```sh
make doc
```
for regular docs, and
```sh
make doc-internal
```
for docs that include private items.
2022-11-13 17:17:42 +00:00
2022-12-09 08:19:53 +00:00
# Maintenance Policies
All on-by-default features of this library are covered by
[semantic versioning][semver] (SemVer). SemVer exemptions are outlined below
for MSRV and public API.
## Minimum Supported Rust Version
| Releases | MSRV |
2022-12-27 10:12:55 +00:00
| :--- |:-------|
| 4.x | 1.60.0 |
2022-12-09 08:19:53 +00:00
| 3.x | 1.41.0 |
From 4.x and on, MSRV changes will be accompanied by a minor version bump.
2022-10-17 19:08:34 +00:00
2022-12-09 08:19:53 +00:00
## Public API SemVer Exemptions
2022-10-17 19:08:34 +00:00
2022-12-09 08:19:53 +00:00
Breaking changes to SemVer exempted components affecting the public API will be accompanied by
_some_ version bump. Below are the specific policies:
2022-10-17 19:08:34 +00:00
2022-12-09 08:19:53 +00:00
| Releases | Public API Component(s) | Policy |
| :--- | :--- | :--- |
| 4.x | Dependencies `digest` and `rand_core` | Minor SemVer bump |
2022-10-17 19:08:34 +00:00
2018-07-12 19:44:56 +00:00
# Safety
The `curve25519-dalek` types are designed to make illegal states
unrepresentable. For example, any instance of an `EdwardsPoint` is
guaranteed to hold a point on the Edwards curve, and any instance of a
`RistrettoPoint` is guaranteed to hold a valid point in the Ristretto
group.
All operations are implemented using constant-time logic (no
secret-dependent branches, no secret-dependent memory accesses),
unless specifically marked as being variable-time code.
2018-07-16 21:30:14 +00:00
We believe that our constant-time logic is lowered to constant-time
assembly, at least on `x86_64` targets.
2019-11-15 22:29:14 +00:00
As an additional guard against possible future compiler optimizations,
the `subtle` crate places an optimization barrier before every
2018-07-16 21:30:14 +00:00
conditional move or assignment. More details can be found in [the
2019-11-15 22:29:14 +00:00
documentation for the `subtle` crate][subtle_doc].
2018-07-12 19:44:56 +00:00
Some functionality (e.g., multiscalar multiplication or batch
2018-07-16 21:30:14 +00:00
inversion) requires heap allocation for temporary buffers. All
2018-07-12 19:44:56 +00:00
heap-allocated buffers of potentially secret data are explicitly
2018-07-16 21:30:14 +00:00
zeroed before release.
2018-07-12 19:44:56 +00:00
However, we do not attempt to zero stack data, for two reasons.
First, it's not possible to do so correctly: we don't have control
over stack allocations, so there's no way to know how much data to
wipe. Second, because `curve25519-dalek` provides a mid-level API,
the correct place to start zeroing stack data is likely not at the
entrypoints of `curve25519-dalek` functions, but at the entrypoints of
functions in other crates.
The implementation is memory-safe, and contains no significant
2019-10-25 22:58:41 +00:00
`unsafe` code. The SIMD backend uses `unsafe` internally to call SIMD
2019-11-15 22:29:14 +00:00
intrinsics. These are marked `unsafe` only because invoking them on an
2019-10-25 22:58:41 +00:00
inappropriate CPU would cause `SIGILL` , but the entire backend is only
2019-11-15 22:29:14 +00:00
compiled with appropriate `target_feature` s, so this cannot occur.
2018-07-12 19:44:56 +00:00
# Performance
2018-03-26 00:10:30 +00:00
Benchmarks are run using [`criterion.rs`][criterion]:
2017-12-02 03:19:19 +00:00
```sh
2022-12-11 21:37:50 +00:00
cargo bench --features "rand_core"
2019-10-25 22:58:41 +00:00
# Uses avx2 or ifma only if compiled for an appropriate target.
2022-12-11 21:37:50 +00:00
export RUSTFLAGS='--cfg curve25519_dalek_backend="simd" -C target_cpu=native'
cargo +nightly bench --features "rand_core"
2017-12-02 03:19:19 +00:00
```
2017-03-14 08:53:26 +00:00
2018-07-12 19:44:56 +00:00
Performance is a secondary goal behind correctness, safety, and
clarity, but we aim to be competitive with other implementations.
2018-07-04 20:54:05 +00:00
2018-08-03 18:00:29 +00:00
# FFI
Unfortunately, we have no plans to add FFI to `curve25519-dalek` directly. The
reason is that we use Rust features to provide an API that maintains safety
invariants, which are not possible to maintain across an FFI boundary. For
instance, as described in the _Safety_ section above, invalid points are
impossible to construct, and this would not be the case if we exposed point
operations over FFI.
However, `curve25519-dalek` is designed as a *mid-level* API, aimed at
implementing other, higher-level primitives. Instead of providing FFI at the
mid-level, our suggestion is to implement the higher-level primitive (a
signature, PAKE, ZKP, etc) in Rust, using `curve25519-dalek` as a dependency,
and have that crate provide a minimal, byte-buffer-oriented FFI specific to
that primitive.
2018-01-25 21:40:30 +00:00
# Contributing
2016-12-08 05:12:00 +00:00
2018-01-25 21:40:30 +00:00
Please see [CONTRIBUTING.md][contributing].
2017-08-01 03:03:11 +00:00
2018-01-25 21:40:30 +00:00
# About
2017-11-16 00:47:01 +00:00
2018-01-25 21:40:30 +00:00
**SPOILER ALERT:** *The Twelfth Doctor's first encounter with the Daleks is in
his second full episode, "Into the Dalek". A beleaguered ship of the "Combined
Galactic Resistance" has discovered a broken Dalek that has turned "good",
desiring to kill all other Daleks. The Doctor, Clara and a team of soldiers
are miniaturized and enter the Dalek, which the Doctor names Rusty. They
repair the damage, but accidentally restore it to its original nature, causing
it to go on the rampage and alert the Dalek fleet to the whereabouts of the
rebel ship. However, the Doctor manages to return Rusty to its previous state
by linking his mind with the Dalek's: Rusty shares the Doctor's view of the
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.*
2017-11-16 00:47:01 +00:00
2018-01-25 21:40:30 +00:00
`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
2018-06-18 20:30:45 +00:00
turn a port of the reference `ref10` implementation. Most of this code,
including the 32-bit field arithmetic, has since been rewritten.
2018-01-25 21:40:30 +00:00
The fast `u32` and `u64` scalar arithmetic was implemented by Andrew Moon, and
2018-07-04 20:59:37 +00:00
the addition chain for scalar inversion was provided by Brian Smith. The
optimised batch inversion was contributed by Sean Bowe and Daira Hopwood.
2018-01-26 02:00:17 +00:00
2019-10-25 22:58:41 +00:00
The `no_std` and `zeroize` support was contributed by Tony Arcieri.
2018-01-26 02:00:17 +00:00
2022-11-13 17:17:42 +00:00
The formally verified `fiat_backend` integrates Rust code generated by the
[Fiat Crypto project ](https://github.com/mit-plv/fiat-crypto ) and was
contributed by François Garillot.
2021-04-14 03:05:51 +00:00
Thanks also to Ashley Hauck, Lucas Salibian, Manish Goregaokar, Jack Grigg,
2022-12-09 08:19:53 +00:00
Pratyush Mishra, Michael Rosenberg, @pinkforest , and countless others for their
2018-01-26 02:00:17 +00:00
contributions.
2018-01-25 21:40:30 +00:00
[ed25519-dalek]: https://github.com/dalek-cryptography/ed25519-dalek
[x25519-dalek]: https://github.com/dalek-cryptography/x25519-dalek
2022-12-09 08:19:53 +00:00
[docs]: https://docs.rs/curve25519-dalek/
2018-01-25 21:40:30 +00:00
[contributing]: https://github.com/dalek-cryptography/curve25519-dalek/blob/master/CONTRIBUTING.md
2018-03-26 00:10:30 +00:00
[criterion]: https://github.com/japaric/criterion.rs
2022-11-24 23:14:25 +00:00
[parallel_doc]: https://docs.rs/curve25519-dalek/latest/curve25519_dalek/backend/vector/index.html
2022-12-07 10:36:07 +00:00
[subtle_doc]: https://docs.rs/subtle
2022-11-13 17:17:42 +00:00
[fiat-crypto]: https://github.com/mit-plv/fiat-crypto
2022-12-09 08:19:53 +00:00
[semver]: https://semver.org/spec/v2.0.0.html
[rngcorestd]: https://github.com/rust-random/rand/tree/7aa25d577e2df84a5156f824077bb7f6bdf28d97/rand_core#crate-features
2022-12-26 21:19:55 +00:00
[zeroize-trait]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html