p521/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_auto_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#![forbid(unsafe_code)]
9#![warn(
10    clippy::mod_module_files,
11    clippy::unwrap_used,
12    missing_docs,
13    rust_2018_idioms,
14    unused_lifetimes,
15    unused_qualifications
16)]
17
18//! ## `serde` support
19//!
20//! When the `serde` feature of this crate is enabled, `Serialize` and
21//! `Deserialize` are impl'd for the following types:
22//!
23//! - [`AffinePoint`]
24//! - [`Scalar`]
25//!
26//! Please see type-specific documentation for more information.
27
28#[cfg(feature = "arithmetic")]
29mod arithmetic;
30
31#[cfg(feature = "ecdh")]
32pub mod ecdh;
33
34#[cfg(feature = "ecdsa-core")]
35pub mod ecdsa;
36
37#[cfg(any(feature = "test-vectors", test))]
38pub mod test_vectors;
39
40#[cfg(feature = "arithmetic")]
41pub use arithmetic::{scalar::Scalar, AffinePoint, ProjectivePoint};
42
43pub use elliptic_curve::{self, bigint::U576};
44
45#[cfg(feature = "pkcs8")]
46pub use elliptic_curve::pkcs8;
47
48use elliptic_curve::{consts::U66, generic_array::GenericArray, FieldBytesEncoding};
49
50/// NIST P-521 elliptic curve.
51#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
52pub struct NistP521;
53
54impl elliptic_curve::Curve for NistP521 {
55    /// 66-byte serialized field elements.
56    type FieldBytesSize = U66;
57
58    /// 521-bit integer type used for internally representing field elements.
59    type Uint = U576;
60
61    /// Order of NIST P-521's elliptic curve group (i.e. scalar modulus).
62    const ORDER: U576 = U576::from_be_hex("00000000000001fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409");
63}
64
65impl elliptic_curve::PrimeCurve for NistP521 {}
66
67impl elliptic_curve::point::PointCompression for NistP521 {
68    /// NIST P-521 points are typically uncompressed.
69    const COMPRESS_POINTS: bool = false;
70}
71
72impl elliptic_curve::point::PointCompaction for NistP521 {
73    /// NIST P-521 points are typically uncompacted.
74    const COMPACT_POINTS: bool = false;
75}
76
77#[cfg(feature = "jwk")]
78impl elliptic_curve::JwkParameters for NistP521 {
79    const CRV: &'static str = "P-521";
80}
81
82#[cfg(feature = "pkcs8")]
83impl pkcs8::AssociatedOid for NistP521 {
84    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.3.132.0.35");
85}
86
87/// Compressed SEC1-encoded NIST P-521 curve point.
88pub type CompressedPoint = GenericArray<u8, U66>;
89
90/// NIST P-521 SEC1 encoded point.
91pub type EncodedPoint = elliptic_curve::sec1::EncodedPoint<NistP521>;
92
93/// NIST P-521 field element serialized as bytes.
94///
95/// Byte array containing a serialized field element value (base field or
96/// scalar).
97pub type FieldBytes = elliptic_curve::FieldBytes<NistP521>;
98
99impl FieldBytesEncoding<NistP521> for U576 {}
100
101/// Non-zero NIST P-521 scalar field element.
102#[cfg(feature = "arithmetic")]
103pub type NonZeroScalar = elliptic_curve::NonZeroScalar<NistP521>;
104
105/// NIST P-521 public key.
106#[cfg(feature = "arithmetic")]
107pub type PublicKey = elliptic_curve::PublicKey<NistP521>;
108
109/// NIST P-521 secret key.
110pub type SecretKey = elliptic_curve::SecretKey<NistP521>;
111
112#[cfg(feature = "voprf")]
113impl elliptic_curve::VoprfParameters for NistP521 {
114    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-19.html#section-4.5-1>.
115    const ID: &'static str = "P521-SHA512";
116
117    /// See <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-08.html#section-4.5-1.2>.
118    type Hash = sha2::Sha512;
119}