generic_ec/
lib.rs

1//! ![License](https://img.shields.io/crates/l/generic-ec.svg)
2//! [![Docs](https://docs.rs/generic-ec/badge.svg)](https://docs.rs/generic-ec)
3//! [![Crates io](https://img.shields.io/crates/v/generic-ec.svg)](https://crates.io/crates/generic-ec)
4//! [![Discord](https://img.shields.io/discord/905194001349627914?logo=discord&logoColor=ffffff&label=Discord)][in Discord]
5//!
6//! # General elliptic curve cryptography
7//!
8//! The library provides a set of simple abstractions boosting experience of doing elliptic curve arithmetic
9//! in Rust. Aim is to **stay simple**, **generic**, and **secure**. It's handy for developers who implement MPC,
10//! zero-knowledge protocols, or any other elliptic crypto algorithms.
11//!
12//! `generic-ec` is `no_std` and web assembly friendly.
13//!
14//! ## Overview
15//!
16//! Crate provides three primitives: a point on elliptic curve [`Point<E>`](Point), an integer modulus group order
17//! [`Scalar<E>`](Scalar), and a secret scalar carrying some sensitive value (e.g. secret key) [`SecretScalar<E>`](SecretScalar).
18//! `E` stands for a choice of elliptic curve, it could be any [supported curve][supported curves], e.g. `Point<Secp256k1>`
19//! is an elliptic point on secp256k1 curve.
20//!
21//! ## Exposed API
22//!
23//! Limited API is exposed: elliptic point arithmetic (points addition, negation, multiplying at scalar), scalar
24//! arithmetic (addition, multiplication, inverse modulo prime group order), and encode/decode to bytes represenstation.
25//!
26//! Hash to curve, hash to scalar primitives, accessing affine coordinates of points are available for some curves through
27//! `FromHash` and other traits.
28//!
29//! ## Security & guarantees
30//!
31//! Library mitigates a bunch of attacks (such as small-group attack) by design by enforcing following checks:
32//! * Scalar `Scalar<E>` must be an integer modulo curve prime order
33//! * Elliptic point `Point<E>` must be on the curve \
34//!   I.e. elliptic point is guaranteed to satisfy equation of `E`
35//! * `Point<E>` is torsion-free \
36//!   Elliptic points should be free of small-group component. This eliminates any kind of small-group attacks.
37//!
38//! Point or scalar not meeting above requirements cannot be constructed (in safe Rust), as these checks are
39//! always enforced. E.g. if you're deserializing a sequence of bytes that represents an invalid point,
40//! deserialization will result into error.
41//!
42//! ### `SecretScalar<E>`
43//!
44//! Sometimes your scalar represents some sensitive value like secret key, and you want to keep it safer.
45//! `SecretScalar<E>` is in-place replacement of `Scalar<E>` that enforces additional security by storing
46//! the scalar value on the heap, and erasing it on drop. Its advantage is that it doesn't leave any trace
47//! in memory dump after it's dropped (which is not guaranteed by regular `Scalar<E>`).
48//!
49//! But keep in mind that we can't control the OS which could potentially load RAM page containing sensitive value
50//! to the swap disk (i.e. on your HDD/SSD) if you're running low on memory. Or it could do any other fancy stuff.
51//! We avoid writing unsafe or OS-specific code that could mitigate this problem.
52//!
53//! ### Points at infinity
54//!
55//! It should be noticed that point at infinity (or identity point) is a valid `Point<E>`. You can construct it by calling
56//! `Point::<E>::zero()`, e.g. `Point::<Secp256k1>::zero()` is a point at infinity for secp256k1 curve.
57//!
58//! If the protocol you're implementing requires points/scalars to be non-zero, you may need to enforce this check by calling
59//! `.is_zero()` method or by using [`NonZero<T>`](NonZero) (`NonZero<Point<E>>` or `NonZero<Scalar<E>>`).
60//!
61//! Using `NonZero<T>` gives some compile-time guarantees. For instance, multiplying non-zero point in the prime group at
62//! non-zero scalar mod group order is mathematically guaranteed to output non-zero point in that prime group. Thus,
63//! multiplying `NonZero<Point<E>>` at `NonZero<Scalar<E>>` returns `NonZero<Point<E>>`.
64//!
65//!
66//! ## Supported curves
67//!
68//! Crate provides support for following elliptic curves out of box:
69//!
70//! | Curve        | Feature            | Backend           |
71//! |--------------|--------------------|-------------------|
72//! | secp256k1    | `curve-secp256k1`  | [RustCrypto/k256] |
73//! | secp256r1    | `curve-secp256r1`  | [RustCrypto/p256] |
74//! | stark-curve  | `curve-stark`      | [Dfns/stark]      |
75//! | Ed25519      | `curve-ed25519`    | [curve25519-dalek]|
76//!
77//! [RustCrypto/k256]: https://github.com/RustCrypto/elliptic-curves/tree/master/k256
78//! [RustCrypto/p256]: https://github.com/RustCrypto/elliptic-curves/tree/master/p256
79//! [Dfns/stark]: https://github.com/LFDT-Lockness/stark-curve/
80//! [curve25519-dalek]: https://docs.rs/curve25519-dalek/
81//!
82//! In order to use one of the supported curves, you need to turn on corresponding feature. E.g. if you want
83//! to use secp256k1 curve, add this to Cargo.toml:
84//!
85//! ```toml
86//! [dependency]
87//! generic-ec = { version = "...", features = ["curve-secp256k1"] }
88//! ```
89//!
90//! And now you can generate a point on that curve:
91//!
92//! ```rust
93//! use generic_ec::{Point, Scalar, curves::Secp256k1};
94//! # let mut rng = rand::rngs::OsRng;
95//!
96//! let random_point: Point<Secp256k1> = Point::generator() * Scalar::random(&mut rng);
97//! ```
98//!
99//! ### Adding support for other curves
100//!
101//! Adding new curve is as easy as implementing [`Curve` trait](Curve)! If you're missing some curve support,
102//! or you're not fine with using existing implementation, you may define your implementation of `Curve` trait
103//! and enjoy using the same handy primitives `Point<YOUR_EC>`, `Scalar<YOUR_EC>`, and etc.
104//!
105//! ## Features
106//!
107//! * `curve-{name}` enables specified curve support. See list of [supported curves].
108//! * `all-curves` enables all supported curves
109//! * `serde` enables points/scalar (de)serialization support. (enabled by default)
110//! * `std` enables support of standard library (enabled by default)
111//!
112//! ## Examples
113//!
114//! ### Random scalar / point generation
115//!
116//! ```rust
117//! use generic_ec::{Point, Scalar, curves::Secp256k1};
118//! # let mut rng = rand::rngs::OsRng;
119//!
120//! // Generates random non-zero scalar
121//! let random_scalar = Scalar::<Secp256k1>::random(&mut rng);
122//! // Produces a point that's result of generator multiplied at the random scalar
123//! let point = Point::generator() * &random_scalar;
124//! ```
125//!
126//! ### Diffie-Hellman key exchange
127//!
128//! ```rust
129//! use generic_ec::{Point, SecretScalar, curves::Secp256k1};
130//! # let mut rng = rand::rngs::OsRng;
131//!
132//! let alice_sk = SecretScalar::<Secp256k1>::random(&mut rng);
133//! let alice_pk = Point::generator() * &alice_sk;
134//!
135//! let bob_sk = SecretScalar::<Secp256k1>::random(&mut rng);
136//! let bob_pk = Point::generator() * &bob_sk;
137//!
138//! let shared_secret_learned_by_alice = bob_pk * &alice_sk;
139//! let shared_secret_learned_by_bob = alice_pk * &bob_sk;
140//! assert_eq!(shared_secret_learned_by_alice, shared_secret_learned_by_bob);
141//! ```
142//!
143//! ### Generic over choice of curve
144//!
145//! You can simply make your function generic over choice of curve:
146//!
147//! ```rust
148//! use generic_ec::{Point, Scalar, Curve};
149//! use rand::RngCore;
150//!
151//! fn some_generic_computation<E: Curve>(rng: &mut impl RngCore, point: Point<E>) -> Point<E> {
152//!     let blinding = Point::<E>::generator() * Scalar::random(rng);
153//!     let e = &point + &blinding;
154//!     // ... some computation
155//!     # e
156//! }
157//!
158//! // You can run this function with any supported curve:
159//! use generic_ec::curves::{Secp256k1, Secp256r1};
160//! # let mut rng = rand::rngs::OsRng;
161//!
162//! let point1 = Point::<Secp256k1>::generator().to_point();
163//! let _ = some_generic_computation(&mut rng, point1);
164//!
165//! let point2 = Point::<Secp256r1>::generator().to_point();
166//! let _ = some_generic_computation(&mut rng, point2);
167//!
168//! // ...
169//! ```
170//!
171//! [examples]: #examples
172//! [supported curves]: #supported-curves
173//!
174//! ## Join us in Discord!
175//! Feel free to reach out to us [in Discord]!
176//!
177//! [in Discord]: https://discordapp.com/channels/905194001349627914/1285268686147424388
178//!
179//! ## License
180//!
181//! The crate is licensed under MIT or Apache-2.0 at your choice.
182
183#![forbid(missing_docs)]
184#![cfg_attr(not(test), forbid(unused_crate_dependencies))]
185#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used))]
186#![no_std]
187#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
188
189#[cfg(feature = "std")]
190extern crate std;
191
192#[cfg(feature = "alloc")]
193extern crate alloc;
194
195pub use generic_ec_core as core;
196
197mod arithmetic;
198pub mod as_raw;
199pub mod coords;
200mod encoded;
201pub mod errors;
202mod generator;
203pub mod multiscalar;
204mod non_zero;
205mod point;
206mod scalar;
207mod secret_scalar;
208
209mod _unused_deps {
210    // This dependency is not used directly without `alloc` feature. Note that
211    // even if `alloc` feature is off, this crate is still present in the
212    // dependency tree as `curve-ed25519` feature is enabled, it's just not
213    // used directly
214    #[cfg(all(feature = "curve-ed25519", not(feature = "alloc")))]
215    use curve25519 as _;
216}
217
218/// Common traits for points and scalars
219pub mod traits {
220    #[doc(inline)]
221    pub use crate::core::{NoInvalidPoints, One, Reduce, Samplable, Zero};
222
223    /// Trait that allows you to check whether value is zero
224    pub trait IsZero {
225        /// Checks whether `self` is zero
226        fn is_zero(&self) -> bool;
227    }
228}
229
230pub mod serde;
231
232pub use self::{
233    core::Curve,
234    encoded::{EncodedPoint, EncodedScalar},
235    generator::Generator,
236    non_zero::definition::NonZero,
237    point::definition::Point,
238    scalar::{Radix16Iter, Scalar},
239    secret_scalar::definition::SecretScalar,
240};
241
242/// Curves supported out of the box
243pub mod curves {
244    #[cfg(feature = "curve-ed25519")]
245    #[cfg_attr(docsrs, doc(cfg(feature = "curve-ed25519")))]
246    pub use generic_ec_curves::Ed25519;
247    #[cfg(feature = "curve-secp256k1")]
248    #[cfg_attr(docsrs, doc(cfg(feature = "curve-secp256k1")))]
249    pub use generic_ec_curves::Secp256k1;
250    #[cfg(feature = "curve-secp256r1")]
251    #[cfg_attr(docsrs, doc(cfg(feature = "curve-secp256r1")))]
252    pub use generic_ec_curves::Secp256r1;
253    #[cfg(feature = "curve-stark")]
254    #[cfg_attr(docsrs, doc(cfg(feature = "curve-stark")))]
255    pub use generic_ec_curves::Stark;
256
257    macro_rules! create_aliases {
258        ($(#[$attr:meta] $mod:ident: $curve:ident),+$(,)?) => {$(
259            /// Aliases for [`
260            #[doc = stringify!($curve)]
261            /// `] curve
262            ///
263            /// This module provides type aliases to [`Point`](crate::Point), [`Scalar`](crate::Scalar), and other types
264            /// instantiated with [`
265            #[doc = stringify!($curve)]
266            /// `]. It might be convenient to use this module when you don't need your code to be generic over choice
267            /// of curve.
268            ///
269            /// ## Example
270            /// The code below only works with [`
271            #[doc = stringify!($curve)]
272            /// `] curve. By using type aliases from
273            #[doc = concat!("[`generic_ec::curves::", stringify!($mod), "`](", stringify!($mod), ")")]
274            /// , we never need to deal with generic parameters.
275            ///
276            /// ```rust
277            #[doc = concat!("use generic_ec::curves::", stringify!($mod), "::{Point, SecretScalar};")]
278            ///
279            /// let mut rng = rand::rngs::OsRng;
280            /// let secret_key = SecretScalar::random(&mut rng);
281            /// let public_key = Point::generator() * &secret_key;
282            /// // ...
283            /// ```
284            #[$attr]
285            pub mod $mod {
286                /// Alias for
287                #[doc = concat!("[", stringify!($curve), "](super::", stringify!($curve), ")")]
288                /// curve
289                pub type E = super::$curve;
290                /// Point on [`
291                #[doc = stringify!($curve)]
292                /// `](E) curve
293                pub type Point = crate::Point<super::$curve>;
294                /// Scalar in [`
295                #[doc = stringify!($curve)]
296                /// `](E) curve large prime subgroup
297                pub type Scalar = crate::Scalar<super::$curve>;
298                /// Secret scalar in [`
299                #[doc = stringify!($curve)]
300                /// `](E) curve large prime subgroup
301                pub type SecretScalar = crate::SecretScalar<super::$curve>;
302                /// Point on [`
303                #[doc = stringify!($curve)]
304                /// `](E) curve encoded as bytes
305                pub type EncodedPoint = crate::EncodedPoint<super::$curve>;
306                /// Scalar in [`
307                #[doc = stringify!($curve)]
308                /// `](E) curve large prime subgroup encoded as bytes
309                pub type EncodedScalar = crate::EncodedScalar<super::$curve>;
310                /// Iterator over scalar coefficients in radix 16 representation of [`
311                #[doc = stringify!($curve)]
312                /// `](E) curve
313                pub type Radix16Iter = crate::Radix16Iter<super::$curve>;
314                /// Generator of [`
315                #[doc = stringify!($curve)]
316                /// `](E) curve
317                pub type Generator = crate::Generator<super::$curve>;
318            }
319        )+};
320    }
321
322    create_aliases! {
323        #[cfg(feature = "curve-secp256k1")]
324        secp256k1: Secp256k1,
325        #[cfg(feature = "curve-secp256r1")]
326        secp256r1: Secp256r1,
327        #[cfg(feature = "curve-stark")]
328        stark: Stark,
329        #[cfg(feature = "curve-ed25519")]
330        ed25519: Ed25519,
331    }
332}