interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / curve25519-dalek / README.md

323 lines · 15993 bytes Raw Blame History Rendered
1
2# curve25519-dalek [![](https://buildstats.info/crate/curve25519-dalek)](https://crates.io/crates/curve25519-dalek) [![](https://img.shields.io/docsrs/curve25519-dalek)](https://docs.rs/curve25519-dalek) [![CI](https://github.com/dalek-cryptography/curve25519-dalek/actions/workflows/curve25519-dalek.yml/badge.svg?branch=main)](https://github.com/dalek-cryptography/curve25519-dalek/actions/workflows/curve25519-dalek.yml)
3
4<p align="center">
5<img
6 alt="dalek-cryptography logo: a dalek with edwards curves as sparkles coming out of its radar-schnozzley blaster thingies"
7 width="200px"
8 src="https://cdn.jsdelivr.net/gh/dalek-cryptography/curve25519-dalek/docs/assets/dalek-logo-clear.png"/>
9</p>
10
11**A pure-Rust implementation of group operations on Ristretto and Curve25519.**
12
13`curve25519-dalek` is a library providing group operations on the Edwards and
14Montgomery forms of Curve25519, and on the prime-order Ristretto group.
15
16`curve25519-dalek` is not intended to provide implementations of any particular
17crypto protocol. Rather, implementations of those protocols (such as
18[`x25519-dalek`][x25519-dalek] and [`ed25519-dalek`][ed25519-dalek]) should use
19`curve25519-dalek` as a library.
20
21`curve25519-dalek` is intended to provide a clean and safe _mid-level_ API for use
22implementing a wide range of ECC-based crypto protocols, such as key agreement,
23signatures, anonymous credentials, rangeproofs, and zero-knowledge proof
24systems.
25
26In particular, `curve25519-dalek` implements Ristretto, which constructs a
27prime-order group from a non-prime-order Edwards curve. This provides the
28speed and safety benefits of Edwards curve arithmetic, without the pitfalls of
29cofactor-related abstraction mismatches.
30
31# Use
32
33## Stable
34
35To import `curve25519-dalek`, add the following to the dependencies section of
36your project's `Cargo.toml`:
37```toml
38curve25519-dalek = "4"
39```
40
41If opting into [SemVer-exempted features](#public-api-semver-exemptions) a range
42can be used to scope the tested compatible version range e.g.:
43```toml
44curve25519-dalek = ">= 4.0, < 4.2"
45```
46
47## Feature Flags
48
49| Feature | Default? | Description |
50| :--- | :---: | :--- |
51| `alloc` | ✓ | Enables Edwards and Ristretto multiscalar multiplication, batch scalar inversion, and batch Ristretto double-and-compress. Also enables `zeroize`. |
52| `zeroize` | ✓ | Enables [`Zeroize`][zeroize-trait] for all scalar and curve point types. |
53| `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. |
54| `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. |
55| `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. |
56| `serde` | | Enables `serde` serialization/deserialization for all the point and scalar types. |
57| `legacy_compatibility`| | Enables `Scalar::from_bits`, which allows the user to build unreduced scalars whose arithmetic is broken. Do not use this unless you know what you're doing. |
58| `group` | | Enables external `group` and `ff` crate traits |
59
60To disable the default features when using `curve25519-dalek` as a dependency,
61add `default-features = false` to the dependency in your `Cargo.toml`. To
62disable it when running `cargo`, add the `--no-default-features` CLI flag.
63
64## Major Version API Changes
65
66Breaking changes for each major version release can be found in
67[`CHANGELOG.md`](CHANGELOG.md), under the "Breaking changes" subheader. The
68latest breaking changes in high level are below:
69
70### Breaking changes in 4.0.0
71
72* Update the MSRV from 1.41 to 1.60
73* Provide SemVer policy
74* Make `digest` and `rand_core` optional features
75* Remove `std` and `nightly` features
76* Replace backend selection - See [CHANGELOG.md](CHANGELOG.md) and [backends](#backends)
77* Replace methods `Scalar::{zero, one}` with constants `Scalar::{ZERO, ONE}`
78* `Scalar::from_canonical_bytes` now returns `CtOption`
79* `Scalar::is_canonical` now returns `Choice`
80* Remove `Scalar::from_bytes_clamped` and `Scalar::reduce`
81* Deprecate and feature-gate `Scalar::from_bits` behind `legacy_compatibility`
82* Deprecate `EdwardsPoint::hash_from_bytes` and rename it
83 `EdwardsPoint::nonspec_map_to_curve`
84* Require including a new trait, `use curve25519_dalek::traits::BasepointTable`
85 whenever using `EdwardsBasepointTable` or `RistrettoBasepointTable`
86
87This release also does a lot of dependency updates and relaxations to unblock upstream build issues.
88
89# Backends
90
91Curve arithmetic is implemented and used by one of the following backends:
92
93| Backend | Selection | Implementation | Bits / Word sizes |
94| :--- | :--- | :--- | :--- |
95| `serial` | Automatic | An optimized, non-parllel implementation | `32` and `64` |
96| `fiat` | Manual | Formally verified field arithmetic from [fiat-crypto] | `32` and `64` |
97| `simd` | Automatic | Intel AVX2 / AVX512 IFMA accelerated backend | `64` only |
98
99At runtime, `curve25519-dalek` selects an arithmetic backend from the set of backends it was compiled to support. For Intel x86-64 targets, unless otherwise specified, it will build itself with `simd` support, and default to `serial` at runtime if the appropriate CPU features aren't detected. See [SIMD backend] for more details.
100
101In the future, `simd` backend may be extended to cover more instruction sets. This change will be non-breaking as this is considered as implementation detail.
102
103## Manual Backend Override
104
105You can force the crate to compile with specific backend support, e.g., `serial` for x86-64 targets to save code size, or `fiat` to force the runtime to use verified code. To do this, set the environment variable:
106```sh
107RUSTFLAGS='--cfg curve25519_dalek_backend="BACKEND"'
108```
109Equivalently, you can write to
110`~/.cargo/config`:
111```toml
112[build]
113rustflags = ['--cfg=curve25519_dalek_backend="BACKEND"']
114```
115More info [here](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags).
116
117Note for contributors: The target backends are not entirely independent of each
118other. The [SIMD backend] directly depends on parts of the serial backend to
119function.
120
121## Bits / Word size
122
123`curve25519-dalek` will automatically choose the word size for the `fiat` and
124`serial` backends, based on the build target.
125For example, building for a 64-bit machine, the default 64 bit word size is
126automatically chosen when either the `serial` or `fiat` backend is selected.
127
128In some targets it might be required to override the word size for better
129performance.
130Backend word size can be overridden for `serial` and `fiat` by setting the
131environment variable:
132```sh
133RUSTFLAGS='--cfg curve25519_dalek_bits="SIZE"'
134```
135`SIZE` is `32` or `64`. As in the above section, this can also be placed
136in `~/.cargo/config`.
137
138Note: The [SIMD backend] requires a word size of 64 bits. Attempting to set bits=32 and backend=`simd` will yield a compile error.
139
140### Cross-compilation
141
142Because backend selection is done by target, cross-compiling will select the correct word size automatically. For example, if a x86-64 Linux machine runs the following commands, `curve25519-dalek` will be compiled with the 32-bit `serial` backend.
143```console
144$ sudo apt install gcc-multilib # (or whatever package manager you use)
145$ rustup target add i686-unknown-linux-gnu
146$ cargo build --target i686-unknown-linux-gnu
147```
148
149## SIMD backend
150
151The specific SIMD backend (AVX512 / AVX2 / `serial` default) is selected automatically at runtime, depending on the currently available CPU features, and whether Rust nightly is being used for compilation. The precise conditions are specified below.
152
153For a given CPU feature, you can also specify an appropriate `-C target_feature` to build a binary which assumes the required SIMD instructions are always available. Don't do this if you don't have a good reason.
154
155| Backend | `RUSTFLAGS` | Requires nightly? |
156| :--- | :--- | :--- |
157| avx2 | `-C target_feature=+avx2` | no |
158| avx512 | `-C target_feature=+avx512ifma,+avx512vl` | yes |
159
160If compiled on a non-nightly compiler, `curve25519-dalek` will not include AVX512 code, and therefore will never select it at runtime.
161
162# Documentation
163
164The semver-stable, public-facing `curve25519-dalek` API is documented [here][docs].
165
166## Building Docs Locally
167
168The `curve25519-dalek` documentation requires a custom HTML header to include
169KaTeX for math support. Unfortunately `cargo doc` does not currently support
170this, but docs can be built using
171```sh
172make doc
173```
174for regular docs, and
175```sh
176make doc-internal
177```
178for docs that include private items.
179
180# Maintenance Policies
181
182All on-by-default features of this library are covered by
183[semantic versioning][semver] (SemVer). SemVer exemptions are outlined below
184for MSRV and public API.
185
186## Minimum Supported Rust Version
187
188| Releases | MSRV |
189| :--- |:-------|
190| 4.x | 1.60.0 |
191| 3.x | 1.41.0 |
192
193From 4.x and on, MSRV changes will be accompanied by a minor version bump.
194
195## Public API SemVer Exemptions
196
197Breaking changes to SemVer-exempted components affecting the public API will be accompanied by
198_some_ version bump. Below are the specific policies:
199
200| Releases | Public API Component(s) | Policy |
201| :--- | :--- | :--- |
202| 4.x | Dependencies `group`, `digest` and `rand_core` | Minor SemVer bump |
203
204# Safety
205
206The `curve25519-dalek` types are designed to make illegal states
207unrepresentable. For example, any instance of an `EdwardsPoint` is
208guaranteed to hold a point on the Edwards curve, and any instance of a
209`RistrettoPoint` is guaranteed to hold a valid point in the Ristretto
210group.
211
212All operations are implemented using constant-time logic (no
213secret-dependent branches, no secret-dependent memory accesses),
214unless specifically marked as being variable-time code.
215We believe that our constant-time logic is lowered to constant-time
216assembly, at least on `x86_64` targets.
217
218As an additional guard against possible future compiler optimizations,
219the `subtle` crate places an optimization barrier before every
220conditional move or assignment. More details can be found in [the
221documentation for the `subtle` crate][subtle_doc].
222
223Some functionality (e.g., multiscalar multiplication or batch
224inversion) requires heap allocation for temporary buffers. All
225heap-allocated buffers of potentially secret data are explicitly
226zeroed before release.
227
228However, we do not attempt to zero stack data, for two reasons.
229First, it's not possible to do so correctly: we don't have control
230over stack allocations, so there's no way to know how much data to
231wipe. Second, because `curve25519-dalek` provides a mid-level API,
232the correct place to start zeroing stack data is likely not at the
233entrypoints of `curve25519-dalek` functions, but at the entrypoints of
234functions in other crates.
235
236The implementation is memory-safe, and contains no significant
237`unsafe` code. The SIMD backend uses `unsafe` internally to call SIMD
238intrinsics. These are marked `unsafe` only because invoking them on an
239inappropriate CPU would cause `SIGILL`, but the entire backend is only
240invoked when the appropriate CPU features are detected at runtime, or
241when the whole program is compiled with the appropriate `target_feature`s.
242
243# Performance
244
245Benchmarks are run using [`criterion.rs`][criterion]:
246
247```sh
248cargo bench --features "rand_core"
249export RUSTFLAGS='-C target_cpu=native'
250cargo +nightly bench --features "rand_core"
251```
252
253Performance is a secondary goal behind correctness, safety, and
254clarity, but we aim to be competitive with other implementations.
255
256# FFI
257
258Unfortunately, we have no plans to add FFI to `curve25519-dalek` directly. The
259reason is that we use Rust features to provide an API that maintains safety
260invariants, which are not possible to maintain across an FFI boundary. For
261instance, as described in the _Safety_ section above, invalid points are
262impossible to construct, and this would not be the case if we exposed point
263operations over FFI.
264
265However, `curve25519-dalek` is designed as a *mid-level* API, aimed at
266implementing other, higher-level primitives. Instead of providing FFI at the
267mid-level, our suggestion is to implement the higher-level primitive (a
268signature, PAKE, ZKP, etc) in Rust, using `curve25519-dalek` as a dependency,
269and have that crate provide a minimal, byte-buffer-oriented FFI specific to
270that primitive.
271
272# Contributing
273
274Please see [CONTRIBUTING.md][contributing].
275
276# About
277
278**SPOILER ALERT:** *The Twelfth Doctor's first encounter with the Daleks is in
279his second full episode, "Into the Dalek". A beleaguered ship of the "Combined
280Galactic Resistance" has discovered a broken Dalek that has turned "good",
281desiring to kill all other Daleks. The Doctor, Clara and a team of soldiers
282are miniaturized and enter the Dalek, which the Doctor names Rusty. They
283repair the damage, but accidentally restore it to its original nature, causing
284it to go on the rampage and alert the Dalek fleet to the whereabouts of the
285rebel ship. However, the Doctor manages to return Rusty to its previous state
286by linking his mind with the Dalek's: Rusty shares the Doctor's view of the
287universe's beauty, but also his deep hatred of the Daleks. Rusty destroys the
288other Daleks and departs the ship, determined to track down and bring an end
289to the Dalek race.*
290
291`curve25519-dalek` is authored by Isis Agora Lovecruft and Henry de Valence.
292
293Portions of this library were originally a port of [Adam Langley's
294Golang ed25519 library](https://github.com/agl/ed25519), which was in
295turn a port of the reference `ref10` implementation. Most of this code,
296including the 32-bit field arithmetic, has since been rewritten.
297
298The fast `u32` and `u64` scalar arithmetic was implemented by Andrew Moon, and
299the addition chain for scalar inversion was provided by Brian Smith. The
300optimised batch inversion was contributed by Sean Bowe and Daira Hopwood.
301
302The `no_std` and `zeroize` support was contributed by Tony Arcieri.
303
304The formally verified `fiat_backend` integrates Rust code generated by the
305[Fiat Crypto project](https://github.com/mit-plv/fiat-crypto) and was
306contributed by François Garillot.
307
308Thanks also to Ashley Hauck, Lucas Salibian, Manish Goregaokar, Jack Grigg,
309Pratyush Mishra, Michael Rosenberg, @pinkforest, and countless others for their
310contributions.
311
312[ed25519-dalek]: https://github.com/dalek-cryptography/curve25519-dalek/tree/main/ed25519-dalek
313[x25519-dalek]: https://github.com/dalek-cryptography/curve25519-dalek/tree/main/x25519-dalek
314[docs]: https://docs.rs/curve25519-dalek/
315[contributing]: https://github.com/dalek-cryptography/curve25519-dalek/blob/master/CONTRIBUTING.md
316[criterion]: https://github.com/japaric/criterion.rs
317[parallel_doc]: https://docs.rs/curve25519-dalek/latest/curve25519_dalek/backend/vector/index.html
318[subtle_doc]: https://docs.rs/subtle
319[fiat-crypto]: https://github.com/mit-plv/fiat-crypto
320[semver]: https://semver.org/spec/v2.0.0.html
321[rngcorestd]: https://github.com/rust-random/rand/tree/7aa25d577e2df84a5156f824077bb7f6bdf28d97/rand_core#crate-features
322[zeroize-trait]: https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html
323[SIMD backend]: #simd-backend