Skip to content

Commit 2fde7a0

Browse files
authored
p224: CurveArithmetic + PrimeCurveParams (RustCrypto#763)
Adds impls of the `CurveArithmetic` and `PrimeCurveArithmetic` traits, the latter of which defines the coefficients of the curve equation as well as the coordinates of the generator point. Constants have been sourced from NIST SP 800-186 § 3.2.1.2: P-224.
1 parent 300ce4f commit 2fde7a0

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

p224/src/arithmetic.rs

+71-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,76 @@
11
//! Pure Rust implementation of group operations on secp224r1.
22
//!
3-
//! Curve parameters can be found in [NIST SP 800-186] § G.1.1: Curve P-384.
3+
//! Curve parameters can be found in [NIST SP 800-186] § 3.2.1.2: P-224.
44
//!
55
//! [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
66
7-
pub mod field;
8-
pub mod scalar;
7+
pub(crate) mod field;
8+
pub(crate) mod scalar;
9+
10+
pub use self::scalar::Scalar;
11+
12+
use self::field::FieldElement;
13+
use crate::NistP224;
14+
use elliptic_curve::{CurveArithmetic, PrimeCurveArithmetic};
15+
use primeorder::{point_arithmetic, PrimeCurveParams};
16+
17+
/// Elliptic curve point in affine coordinates.
18+
pub type AffinePoint = primeorder::AffinePoint<NistP224>;
19+
20+
/// Elliptic curve point in projective coordinates.
21+
pub type ProjectivePoint = primeorder::ProjectivePoint<NistP224>;
22+
23+
impl CurveArithmetic for NistP224 {
24+
type AffinePoint = AffinePoint;
25+
type ProjectivePoint = ProjectivePoint;
26+
type Scalar = Scalar;
27+
}
28+
29+
impl PrimeCurveArithmetic for NistP224 {
30+
type CurveGroup = ProjectivePoint;
31+
}
32+
33+
/// Adapted from [NIST SP 800-186] § 3.2.1.2: P-224.
34+
///
35+
/// [NIST SP 800-186]: https://csrc.nist.gov/publications/detail/sp/800-186/final
36+
impl PrimeCurveParams for NistP224 {
37+
type FieldElement = FieldElement;
38+
type PointArithmetic = point_arithmetic::EquationAIsMinusThree;
39+
40+
/// a = -3 (=0xffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe)
41+
const EQUATION_A: FieldElement = FieldElement::from_u64(3).neg();
42+
43+
/// b = 0xb4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4
44+
#[cfg(target_pointer_width = "32")]
45+
const EQUATION_B: FieldElement =
46+
FieldElement::from_hex("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4");
47+
48+
/// b = 0xb4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4
49+
#[cfg(target_pointer_width = "64")]
50+
const EQUATION_B: FieldElement =
51+
FieldElement::from_hex("00000000b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4");
52+
53+
/// Base point of P-224.
54+
///
55+
/// ```text
56+
/// Gₓ = 0xb70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21
57+
/// Gᵧ = 0xbd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34
58+
/// ```
59+
#[cfg(target_pointer_width = "32")]
60+
const GENERATOR: (FieldElement, FieldElement) = (
61+
FieldElement::from_hex("b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
62+
FieldElement::from_hex("bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
63+
);
64+
65+
/// Base point of P-224.
66+
///
67+
/// ```text
68+
/// Gₓ = 0xb70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21
69+
/// Gᵧ = 0xbd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34
70+
/// ```
71+
#[cfg(target_pointer_width = "64")]
72+
const GENERATOR: (FieldElement, FieldElement) = (
73+
FieldElement::from_hex("00000000b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
74+
FieldElement::from_hex("00000000bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
75+
);
76+
}

p224/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use elliptic_curve::{
2929
FieldBytesEncoding,
3030
};
3131

32+
#[cfg(feature = "wip-arithmetic-do-not-use")]
33+
pub use arithmetic::{scalar::Scalar, AffinePoint, ProjectivePoint};
34+
3235
#[cfg(target_pointer_width = "32")]
3336
pub use elliptic_curve::bigint::U224 as Uint;
3437

@@ -89,7 +92,7 @@ impl FieldBytesEncoding<NistP224> for Uint {}
8992
/// NIST P-224 secret key.
9093
pub type SecretKey = elliptic_curve::SecretKey<NistP224>;
9194

92-
#[cfg(not(feature = "arithmetic"))]
95+
#[cfg(not(feature = "wip-arithmetic-do-not-use"))]
9396
impl elliptic_curve::sec1::ValidatePublicKey for NistP224 {}
9497

9598
/// Bit representation of a NIST P-224 scalar field element.

0 commit comments

Comments
 (0)