Skip to main content

p521/
arithmetic.rs

1//! Pure Rust implementation of group operations on secp521r1.
2//!
3//! Curve parameters can be found in [NIST SP 800-186] § 3.2.1.5: P-521.
4//!
5//! [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
6
7pub(crate) mod field;
8pub(crate) mod scalar;
9
10#[cfg(feature = "hash2curve")]
11mod hash2curve;
12#[cfg(feature = "precomputed-tables")]
13mod tables;
14
15pub use self::scalar::Scalar;
16
17use self::field::FieldElement;
18use crate::NistP521;
19use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
20use primeorder::{PrimeCurveParams, point_arithmetic};
21
22/// Elliptic curve point in affine coordinates.
23pub type AffinePoint = primeorder::AffinePoint<NistP521>;
24
25/// Elliptic curve point in projective coordinates.
26pub type ProjectivePoint = primeorder::ProjectivePoint<NistP521>;
27
28impl CurveArithmetic for NistP521 {
29    type AffinePoint = AffinePoint;
30    type ProjectivePoint = ProjectivePoint;
31    type Scalar = Scalar;
32}
33
34impl FieldArithmetic for NistP521 {
35    type FieldElement = FieldElement;
36}
37
38impl PrimeCurveArithmetic for NistP521 {
39    type CurveGroup = ProjectivePoint;
40}
41
42/// Adapted from [NIST SP 800-186] § 3.2.1.5: P-521.
43///
44/// [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
45impl PrimeCurveParams for NistP521 {
46    type PointArithmetic = point_arithmetic::EquationAIsMinusThree;
47
48    #[cfg(not(feature = "precomputed-tables"))]
49    type Backend = primeorder::mul_backend::VariableOnly;
50    // TODO(tarcieri): use `primeorder::mul_backend::PrecomputedTables` when MSRV 1.90
51    #[cfg(feature = "precomputed-tables")]
52    type Backend = tables::backend::PrecomputedTables;
53
54    /// a = -3 (0x1ff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
55    ///               ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
56    ///               ffffffff ffffffff ffffffff fffffffc)
57    const EQUATION_A: FieldElement = FieldElement::from_u64(3).neg();
58
59    /// b = 0x051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b 99b315f3
60    ///           b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd 3bb1bf07
61    ///           3573df88 3d2c34f1 ef451fd4 6b503f00
62    const EQUATION_B: FieldElement = FieldElement::from_hex(
63        "0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
64    );
65
66    /// Base point of P-521.
67    ///
68    /// ```text
69    /// Gₓ = 0x0c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 053fb521
70    ///            f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 a2ffa8de
71    ///            3348b3c1 856a429b f97e7e31 c2e5bd66
72    /// Gᵧ = 0x118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 579b4468
73    ///            17afbd17 273e662c 97ee7299 5ef42640 c550b901 3fad0761
74    ///            353c7086 a272c240 88be9476 9fd16650
75    /// ```
76    const GENERATOR: (FieldElement, FieldElement) = (
77        FieldElement::from_hex(
78            "00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
79        ),
80        FieldElement::from_hex(
81            "011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
82        ),
83    );
84}