aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / curve25519-dalek / README.md
| 1 | |
| 2 | # curve25519-dalek [](https://crates.io/crates/curve25519-dalek) [](https://docs.rs/curve25519-dalek) [](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 |
| 14 | Montgomery forms of Curve25519, and on the prime-order Ristretto group. |
| 15 | |
| 16 | `curve25519-dalek` is not intended to provide implementations of any particular |
| 17 | crypto 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 |
| 22 | implementing a wide range of ECC-based crypto protocols, such as key agreement, |
| 23 | signatures, anonymous credentials, rangeproofs, and zero-knowledge proof |
| 24 | systems. |
| 25 | |
| 26 | In particular, `curve25519-dalek` implements Ristretto, which constructs a |
| 27 | prime-order group from a non-prime-order Edwards curve. This provides the |
| 28 | speed and safety benefits of Edwards curve arithmetic, without the pitfalls of |
| 29 | cofactor-related abstraction mismatches. |
| 30 | |
| 31 | # Use |
| 32 | |
| 33 | ## Stable |
| 34 | |
| 35 | To import `curve25519-dalek`, add the following to the dependencies section of |
| 36 | your project's `Cargo.toml`: |
| 37 | ```toml |
| 38 | curve25519-dalek = "4" |
| 39 | ``` |
| 40 | |
| 41 | If opting into [SemVer-exempted features](#public-api-semver-exemptions) a range |
| 42 | can be used to scope the tested compatible version range e.g.: |
| 43 | ```toml |
| 44 | curve25519-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 | |
| 60 | To disable the default features when using `curve25519-dalek` as a dependency, |
| 61 | add `default-features = false` to the dependency in your `Cargo.toml`. To |
| 62 | disable it when running `cargo`, add the `--no-default-features` CLI flag. |
| 63 | |
| 64 | ## Major Version API Changes |
| 65 | |
| 66 | Breaking changes for each major version release can be found in |
| 67 | [`CHANGELOG.md`](CHANGELOG.md), under the "Breaking changes" subheader. The |
| 68 | latest 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 | |
| 87 | This release also does a lot of dependency updates and relaxations to unblock upstream build issues. |
| 88 | |
| 89 | # Backends |
| 90 | |
| 91 | Curve 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 | |
| 99 | At 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 | |
| 101 | In 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 | |
| 105 | You 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 |
| 107 | RUSTFLAGS='--cfg curve25519_dalek_backend="BACKEND"' |
| 108 | ``` |
| 109 | Equivalently, you can write to |
| 110 | `~/.cargo/config`: |
| 111 | ```toml |
| 112 | [build] |
| 113 | rustflags = ['--cfg=curve25519_dalek_backend="BACKEND"'] |
| 114 | ``` |
| 115 | More info [here](https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags). |
| 116 | |
| 117 | Note for contributors: The target backends are not entirely independent of each |
| 118 | other. The [SIMD backend] directly depends on parts of the serial backend to |
| 119 | function. |
| 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. |
| 125 | For example, building for a 64-bit machine, the default 64 bit word size is |
| 126 | automatically chosen when either the `serial` or `fiat` backend is selected. |
| 127 | |
| 128 | In some targets it might be required to override the word size for better |
| 129 | performance. |
| 130 | Backend word size can be overridden for `serial` and `fiat` by setting the |
| 131 | environment variable: |
| 132 | ```sh |
| 133 | RUSTFLAGS='--cfg curve25519_dalek_bits="SIZE"' |
| 134 | ``` |
| 135 | `SIZE` is `32` or `64`. As in the above section, this can also be placed |
| 136 | in `~/.cargo/config`. |
| 137 | |
| 138 | Note: 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 | |
| 142 | Because 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 | |
| 151 | The 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 | |
| 153 | For 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 | |
| 160 | If 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 | |
| 164 | The semver-stable, public-facing `curve25519-dalek` API is documented [here][docs]. |
| 165 | |
| 166 | ## Building Docs Locally |
| 167 | |
| 168 | The `curve25519-dalek` documentation requires a custom HTML header to include |
| 169 | KaTeX for math support. Unfortunately `cargo doc` does not currently support |
| 170 | this, but docs can be built using |
| 171 | ```sh |
| 172 | make doc |
| 173 | ``` |
| 174 | for regular docs, and |
| 175 | ```sh |
| 176 | make doc-internal |
| 177 | ``` |
| 178 | for docs that include private items. |
| 179 | |
| 180 | # Maintenance Policies |
| 181 | |
| 182 | All on-by-default features of this library are covered by |
| 183 | [semantic versioning][semver] (SemVer). SemVer exemptions are outlined below |
| 184 | for 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 | |
| 193 | From 4.x and on, MSRV changes will be accompanied by a minor version bump. |
| 194 | |
| 195 | ## Public API SemVer Exemptions |
| 196 | |
| 197 | Breaking 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 | |
| 206 | The `curve25519-dalek` types are designed to make illegal states |
| 207 | unrepresentable. For example, any instance of an `EdwardsPoint` is |
| 208 | guaranteed 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 |
| 210 | group. |
| 211 | |
| 212 | All operations are implemented using constant-time logic (no |
| 213 | secret-dependent branches, no secret-dependent memory accesses), |
| 214 | unless specifically marked as being variable-time code. |
| 215 | We believe that our constant-time logic is lowered to constant-time |
| 216 | assembly, at least on `x86_64` targets. |
| 217 | |
| 218 | As an additional guard against possible future compiler optimizations, |
| 219 | the `subtle` crate places an optimization barrier before every |
| 220 | conditional move or assignment. More details can be found in [the |
| 221 | documentation for the `subtle` crate][subtle_doc]. |
| 222 | |
| 223 | Some functionality (e.g., multiscalar multiplication or batch |
| 224 | inversion) requires heap allocation for temporary buffers. All |
| 225 | heap-allocated buffers of potentially secret data are explicitly |
| 226 | zeroed before release. |
| 227 | |
| 228 | However, we do not attempt to zero stack data, for two reasons. |
| 229 | First, it's not possible to do so correctly: we don't have control |
| 230 | over stack allocations, so there's no way to know how much data to |
| 231 | wipe. Second, because `curve25519-dalek` provides a mid-level API, |
| 232 | the correct place to start zeroing stack data is likely not at the |
| 233 | entrypoints of `curve25519-dalek` functions, but at the entrypoints of |
| 234 | functions in other crates. |
| 235 | |
| 236 | The implementation is memory-safe, and contains no significant |
| 237 | `unsafe` code. The SIMD backend uses `unsafe` internally to call SIMD |
| 238 | intrinsics. These are marked `unsafe` only because invoking them on an |
| 239 | inappropriate CPU would cause `SIGILL`, but the entire backend is only |
| 240 | invoked when the appropriate CPU features are detected at runtime, or |
| 241 | when the whole program is compiled with the appropriate `target_feature`s. |
| 242 | |
| 243 | # Performance |
| 244 | |
| 245 | Benchmarks are run using [`criterion.rs`][criterion]: |
| 246 | |
| 247 | ```sh |
| 248 | cargo bench --features "rand_core" |
| 249 | export RUSTFLAGS='-C target_cpu=native' |
| 250 | cargo +nightly bench --features "rand_core" |
| 251 | ``` |
| 252 | |
| 253 | Performance is a secondary goal behind correctness, safety, and |
| 254 | clarity, but we aim to be competitive with other implementations. |
| 255 | |
| 256 | # FFI |
| 257 | |
| 258 | Unfortunately, we have no plans to add FFI to `curve25519-dalek` directly. The |
| 259 | reason is that we use Rust features to provide an API that maintains safety |
| 260 | invariants, which are not possible to maintain across an FFI boundary. For |
| 261 | instance, as described in the _Safety_ section above, invalid points are |
| 262 | impossible to construct, and this would not be the case if we exposed point |
| 263 | operations over FFI. |
| 264 | |
| 265 | However, `curve25519-dalek` is designed as a *mid-level* API, aimed at |
| 266 | implementing other, higher-level primitives. Instead of providing FFI at the |
| 267 | mid-level, our suggestion is to implement the higher-level primitive (a |
| 268 | signature, PAKE, ZKP, etc) in Rust, using `curve25519-dalek` as a dependency, |
| 269 | and have that crate provide a minimal, byte-buffer-oriented FFI specific to |
| 270 | that primitive. |
| 271 | |
| 272 | # Contributing |
| 273 | |
| 274 | Please see [CONTRIBUTING.md][contributing]. |
| 275 | |
| 276 | # About |
| 277 | |
| 278 | **SPOILER ALERT:** *The Twelfth Doctor's first encounter with the Daleks is in |
| 279 | his second full episode, "Into the Dalek". A beleaguered ship of the "Combined |
| 280 | Galactic Resistance" has discovered a broken Dalek that has turned "good", |
| 281 | desiring to kill all other Daleks. The Doctor, Clara and a team of soldiers |
| 282 | are miniaturized and enter the Dalek, which the Doctor names Rusty. They |
| 283 | repair the damage, but accidentally restore it to its original nature, causing |
| 284 | it to go on the rampage and alert the Dalek fleet to the whereabouts of the |
| 285 | rebel ship. However, the Doctor manages to return Rusty to its previous state |
| 286 | by linking his mind with the Dalek's: Rusty shares the Doctor's view of the |
| 287 | universe's beauty, but also his deep hatred of the Daleks. Rusty destroys the |
| 288 | other Daleks and departs the ship, determined to track down and bring an end |
| 289 | to the Dalek race.* |
| 290 | |
| 291 | `curve25519-dalek` is authored by Isis Agora Lovecruft and Henry de Valence. |
| 292 | |
| 293 | Portions of this library were originally a port of [Adam Langley's |
| 294 | Golang ed25519 library](https://github.com/agl/ed25519), which was in |
| 295 | turn a port of the reference `ref10` implementation. Most of this code, |
| 296 | including the 32-bit field arithmetic, has since been rewritten. |
| 297 | |
| 298 | The fast `u32` and `u64` scalar arithmetic was implemented by Andrew Moon, and |
| 299 | the addition chain for scalar inversion was provided by Brian Smith. The |
| 300 | optimised batch inversion was contributed by Sean Bowe and Daira Hopwood. |
| 301 | |
| 302 | The `no_std` and `zeroize` support was contributed by Tony Arcieri. |
| 303 | |
| 304 | The formally verified `fiat_backend` integrates Rust code generated by the |
| 305 | [Fiat Crypto project](https://github.com/mit-plv/fiat-crypto) and was |
| 306 | contributed by François Garillot. |
| 307 | |
| 308 | Thanks also to Ashley Hauck, Lucas Salibian, Manish Goregaokar, Jack Grigg, |
| 309 | Pratyush Mishra, Michael Rosenberg, @pinkforest, and countless others for their |
| 310 | contributions. |
| 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 |