Skip to main content

p384/
arithmetic.rs

1//! Pure Rust implementation of group operations on secp384r1.
2//!
3//! Curve parameters can be found in [NIST SP 800-186] § G.1.3: Curve P-384.
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::NistP384;
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<NistP384>;
22
23/// Elliptic curve point in projective coordinates.
24pub type ProjectivePoint = primeorder::ProjectivePoint<NistP384>;
25
26impl CurveArithmetic for NistP384 {
27    type AffinePoint = AffinePoint;
28    type ProjectivePoint = ProjectivePoint;
29    type Scalar = Scalar;
30}
31
32impl FieldArithmetic for NistP384 {
33    type FieldElement = FieldElement;
34}
35
36impl PrimeCurveArithmetic for NistP384 {
37    type CurveGroup = ProjectivePoint;
38}
39
40/// Adapted from [NIST SP 800-186] § G.1.3: Curve P-384.
41///
42/// [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
43impl PrimeCurveParams for NistP384 {
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 (0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc)
53    const EQUATION_A: FieldElement = FieldElement::from_u64(3).neg();
54
55    /// b = b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112
56    ///     0314088f 5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef
57    const EQUATION_B: FieldElement = FieldElement::from_hex_vartime(
58        "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef",
59    );
60
61    /// Base point of P-384.
62    ///
63    /// Defined in NIST SP 800-186 § G.1.3: Curve P-384.
64    ///
65    /// ```text
66    /// Gₓ = aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98
67    ///      59f741e0 82542a38 5502f25d bf55296c 3a545e38 72760ab7
68    /// Gᵧ = 3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c
69    ///      e9da3113 b5f0b8c0 0a60b1ce 1d7e819d 7a431d7c 90ea0e5f
70    /// ```
71    const GENERATOR: (FieldElement, FieldElement) = (
72        FieldElement::from_hex_vartime(
73            "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7",
74        ),
75        FieldElement::from_hex_vartime(
76            "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f",
77        ),
78    );
79}