Skip to content

Commit 6f1839a

Browse files
committed
elliptic-curve: extract scalar macros from primeorder
Extracts macros for writing `From` and `Mul` impls for scalar types. It would be nice to use these with `ed448-goldilocks` which isn't a prime order curve, and really these macros work for any elliptic curve, not just prime order curves (`primeorder` was previously just a convenient place to put them).
1 parent 24b68fa commit 6f1839a

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

elliptic-curve/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub mod weierstrass;
102102

103103
mod error;
104104
mod field;
105+
mod macros;
105106
mod secret_key;
106107

107108
#[cfg(feature = "arithmetic")]

elliptic-curve/src/macros.rs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//! Macros for writing common patterns that interact with this crate.
2+
3+
/// Writes all impls for scalar field types.
4+
#[macro_export]
5+
macro_rules! scalar_impls {
6+
($curve:path, $scalar:ty) => {
7+
$crate::scalar_from_impls!($curve, $scalar);
8+
$crate::scalar_mul_impls!($curve, $scalar);
9+
};
10+
}
11+
12+
/// Writes a series of `From` impls for scalar field types.
13+
#[macro_export]
14+
macro_rules! scalar_from_impls {
15+
($curve:path, $scalar:ty) => {
16+
impl From<$crate::NonZeroScalar<$curve>> for $scalar {
17+
fn from(scalar: $crate::NonZeroScalar<$curve>) -> Self {
18+
*scalar.as_ref()
19+
}
20+
}
21+
22+
impl From<&$crate::NonZeroScalar<$curve>> for $scalar {
23+
fn from(scalar: &$crate::NonZeroScalar<$curve>) -> Self {
24+
*scalar.as_ref()
25+
}
26+
}
27+
28+
impl From<$crate::ScalarPrimitive<$curve>> for $scalar {
29+
fn from(w: $crate::ScalarPrimitive<$curve>) -> Self {
30+
<$scalar>::from(&w)
31+
}
32+
}
33+
34+
impl From<&$crate::ScalarPrimitive<$curve>> for $scalar {
35+
fn from(w: &$crate::ScalarPrimitive<$curve>) -> $scalar {
36+
<$scalar>::from_uint_unchecked(*w.as_uint())
37+
}
38+
}
39+
40+
impl From<$scalar> for $crate::ScalarPrimitive<$curve> {
41+
fn from(scalar: $scalar) -> $crate::ScalarPrimitive<$curve> {
42+
$crate::ScalarPrimitive::from(&scalar)
43+
}
44+
}
45+
46+
impl From<&$scalar> for $crate::ScalarPrimitive<$curve> {
47+
fn from(scalar: &$scalar) -> $crate::ScalarPrimitive<$curve> {
48+
$crate::ScalarPrimitive::new(scalar.into()).unwrap()
49+
}
50+
}
51+
52+
impl From<&$crate::SecretKey<$curve>> for $scalar {
53+
fn from(secret_key: &$crate::SecretKey<$curve>) -> $scalar {
54+
*secret_key.to_nonzero_scalar()
55+
}
56+
}
57+
58+
/// The constant-time alternative is available at [`$crate::NonZeroScalar<$curve>::new()`].
59+
impl TryFrom<$scalar> for $crate::NonZeroScalar<$curve> {
60+
type Error = $crate::Error;
61+
62+
fn try_from(scalar: $scalar) -> $crate::Result<Self> {
63+
$crate::NonZeroScalar::new(scalar)
64+
.into_option()
65+
.ok_or($crate::Error)
66+
}
67+
}
68+
};
69+
}
70+
71+
/// Writes a series of `Mul` impls for an elliptic curve's scalar field
72+
#[macro_export]
73+
macro_rules! scalar_mul_impls {
74+
($curve:path, $scalar:ty) => {
75+
impl ::core::ops::Mul<$crate::AffinePoint<$curve>> for $scalar {
76+
type Output = $crate::ProjectivePoint<$curve>;
77+
78+
#[inline]
79+
fn mul(self, rhs: $crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
80+
rhs * self
81+
}
82+
}
83+
84+
impl ::core::ops::Mul<&$crate::AffinePoint<$curve>> for $scalar {
85+
type Output = $crate::ProjectivePoint<$curve>;
86+
87+
#[inline]
88+
fn mul(self, rhs: &$crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
89+
*rhs * self
90+
}
91+
}
92+
93+
impl ::core::ops::Mul<$crate::AffinePoint<$curve>> for &$scalar {
94+
type Output = $crate::ProjectivePoint<$curve>;
95+
96+
#[inline]
97+
fn mul(self, rhs: $crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
98+
rhs * self
99+
}
100+
}
101+
102+
impl ::core::ops::Mul<&$crate::AffinePoint<$curve>> for &$scalar {
103+
type Output = $crate::ProjectivePoint<$curve>;
104+
105+
#[inline]
106+
fn mul(self, rhs: &$crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
107+
*rhs * self
108+
}
109+
}
110+
111+
impl ::core::ops::Mul<$crate::ProjectivePoint<$curve>> for $scalar {
112+
type Output = $crate::ProjectivePoint<$curve>;
113+
114+
#[inline]
115+
fn mul(self, rhs: $crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
116+
rhs * self
117+
}
118+
}
119+
120+
impl ::core::ops::Mul<&$crate::ProjectivePoint<$curve>> for $scalar {
121+
type Output = $crate::ProjectivePoint<$curve>;
122+
123+
#[inline]
124+
fn mul(self, rhs: &$crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
125+
rhs * &self
126+
}
127+
}
128+
129+
impl ::core::ops::Mul<$crate::ProjectivePoint<$curve>> for &$scalar {
130+
type Output = $crate::ProjectivePoint<$curve>;
131+
132+
#[inline]
133+
fn mul(self, rhs: $crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
134+
rhs * self
135+
}
136+
}
137+
138+
impl ::core::ops::Mul<&$crate::ProjectivePoint<$curve>> for &$scalar {
139+
type Output = $crate::ProjectivePoint<$curve>;
140+
141+
#[inline]
142+
fn mul(self, rhs: &$crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
143+
rhs * self
144+
}
145+
}
146+
};
147+
}

0 commit comments

Comments
 (0)