Skip to main content

p256/
arithmetic.rs

1//! Pure Rust implementation of group operations on secp256r1.
2//!
3//! Curve parameters can be found in [NIST SP 800-186] § G.1.2: Curve P-256.
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
15use self::{field::FieldElement, scalar::Scalar};
16use crate::NistP256;
17use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic, hazmat::FieldArithmetic};
18use primeorder::{PrimeCurveParams, point_arithmetic};
19
20/// Elliptic curve point in affine coordinates.
21pub type AffinePoint = primeorder::AffinePoint<NistP256>;
22
23/// Elliptic curve point in projective coordinates.
24pub type ProjectivePoint = primeorder::ProjectivePoint<NistP256>;
25
26impl CurveArithmetic for NistP256 {
27    type AffinePoint = AffinePoint;
28    type ProjectivePoint = ProjectivePoint;
29    type Scalar = Scalar;
30}
31
32impl FieldArithmetic for NistP256 {
33    type FieldElement = FieldElement;
34}
35
36impl PrimeCurveArithmetic for NistP256 {
37    type CurveGroup = ProjectivePoint;
38}
39
40/// Adapted from [NIST SP 800-186] § G.1.2: Curve P-256.
41///
42/// [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
43impl PrimeCurveParams for NistP256 {
44    type PointArithmetic = point_arithmetic::EquationAIsMinusThree;
45
46    #[cfg(not(feature = "precomputed-tables"))]
47    type Backend = primeorder::mul_backend::VariableOnly;
48    // TODO(tarcieri): use `primeorder::mul_backend::PrecomputedTables` when MSRV 1.90
49    #[cfg(feature = "precomputed-tables")]
50    type Backend = tables::backend::PrecomputedTables;
51
52    /// a = -3
53    const EQUATION_A: FieldElement = FieldElement::from_u64(3).neg();
54
55    const EQUATION_B: FieldElement = FieldElement::from_hex_vartime(
56        "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
57    );
58
59    /// Base point of P-256.
60    ///
61    /// Defined in NIST SP 800-186 § G.1.2:
62    ///
63    /// ```text
64    /// Gₓ = 6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296
65    /// Gᵧ = 4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5
66    /// ```
67    const GENERATOR: (FieldElement, FieldElement) = (
68        FieldElement::from_hex_vartime(
69            "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
70        ),
71        FieldElement::from_hex_vartime(
72            "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
73        ),
74    );
75}