Skip to main content

k256/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc = include_str!("../README.md")]
4#![doc(
5    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
6    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
7)]
8#![allow(clippy::needless_range_loop)]
9#![forbid(unsafe_code)]
10#![warn(
11    clippy::mod_module_files,
12    clippy::unwrap_used,
13    missing_docs,
14    rust_2018_idioms,
15    unused_lifetimes,
16    unused_qualifications
17)]
18
19//! ## `serde` support
20//!
21//! When the `serde` feature of this crate is enabled, `Serialize` and
22//! `Deserialize` are impl'd for the following types:
23//!
24//! - [`AffinePoint`]
25//! - [`Scalar`]
26//! - [`ecdsa::VerifyingKey`]
27//!
28//! Please see type-specific documentation for more information.
29
30#[cfg(feature = "alloc")]
31#[allow(unused_imports)]
32#[macro_use]
33extern crate alloc;
34#[cfg(feature = "std")]
35extern crate std;
36
37#[cfg(feature = "arithmetic")]
38mod arithmetic;
39
40#[cfg(feature = "ecdh")]
41pub mod ecdh;
42
43#[cfg(feature = "ecdsa-core")]
44pub mod ecdsa;
45
46#[cfg(feature = "schnorr")]
47pub mod schnorr;
48
49#[cfg(any(feature = "test-vectors", test))]
50pub mod test_vectors;
51
52#[cfg(feature = "hash2curve")]
53pub use hash2curve;
54
55pub use elliptic_curve::{self, bigint::U256};
56
57#[cfg(feature = "arithmetic")]
58pub use arithmetic::{affine::AffinePoint, projective::ProjectivePoint, scalar::Scalar};
59#[cfg(feature = "pkcs8")]
60pub use elliptic_curve::pkcs8;
61#[cfg(feature = "sha2")]
62pub use sha2;
63
64use elliptic_curve::{
65    array::Array,
66    bigint::Odd,
67    consts::{U32, U33, U64},
68};
69
70/// Order of the secp256k1 elliptic curve in hexadecimal.
71const ORDER_HEX: &str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141";
72
73/// Order of the secp256k1 elliptic curve.
74const ORDER: Odd<U256> = Odd::<U256>::from_be_hex(ORDER_HEX);
75
76/// secp256k1 (K-256) elliptic curve.
77///
78/// Specified in Certicom's SECG in "SEC 2: Recommended Elliptic Curve Domain Parameters":
79///
80/// <https://www.secg.org/sec2-v2.pdf>
81///
82/// The curve's equation is `y² = x³ + 7` over a ~256-bit prime field.
83///
84/// It's primarily notable for usage in Bitcoin and other cryptocurrencies,
85/// particularly in conjunction with the Elliptic Curve Digital Signature
86/// Algorithm (ECDSA).
87#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
88pub struct Secp256k1;
89
90impl elliptic_curve::Curve for Secp256k1 {
91    /// 32-byte serialized field elements.
92    type FieldBytesSize = U32;
93
94    /// 256-bit field modulus.
95    type Uint = U256;
96
97    /// Curve order.
98    const ORDER: Odd<U256> = ORDER;
99}
100
101impl elliptic_curve::PrimeCurve for Secp256k1 {}
102
103impl elliptic_curve::point::PointCompression for Secp256k1 {
104    /// secp256k1 points are typically compressed.
105    const COMPRESS_POINTS: bool = true;
106}
107
108#[cfg(feature = "pkcs8")]
109impl pkcs8::AssociatedOid for Secp256k1 {
110    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.3.132.0.10");
111}
112
113/// Compressed SEC1-encoded secp256k1 (K-256) curve point.
114pub type CompressedPoint = Array<u8, U33>;
115
116/// SEC1-encoded secp256k1 (K-256) curve point.
117pub type Sec1Point = elliptic_curve::sec1::Sec1Point<Secp256k1>;
118
119/// secp256k1 (K-256) field element serialized as bytes.
120///
121/// Byte array containing a serialized field element value (base field or scalar).
122pub type FieldBytes = elliptic_curve::FieldBytes<Secp256k1>;
123
124/// Bytes used by a wide reduction: twice the width of [`FieldBytes`].
125pub type WideBytes = Array<u8, U64>;
126
127/// Non-zero secp256k1 (K-256) scalar field element.
128#[cfg(feature = "arithmetic")]
129pub type NonZeroScalar = elliptic_curve::NonZeroScalar<Secp256k1>;
130
131/// secp256k1 (K-256) public key.
132#[cfg(feature = "arithmetic")]
133pub type PublicKey = elliptic_curve::PublicKey<Secp256k1>;
134
135/// secp256k1 (K-256) secret key.
136pub type SecretKey = elliptic_curve::SecretKey<Secp256k1>;
137
138#[cfg(not(feature = "arithmetic"))]
139impl elliptic_curve::sec1::ValidatePublicKey for Secp256k1 {}