Skip to main content

elliptic_curve/
macros.rs

1//! Macros for writing common patterns that interact with this crate.
2
3/// Writes all impls for scalar field types.
4#[macro_export]
5macro_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]
14macro_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::ScalarValue<$curve>> for $scalar {
29            fn from(w: $crate::ScalarValue<$curve>) -> Self {
30                <$scalar>::from(&w)
31            }
32        }
33
34        impl From<&$crate::ScalarValue<$curve>> for $scalar {
35            fn from(w: &$crate::ScalarValue<$curve>) -> $scalar {
36                <$scalar>::from_uint_unchecked(*w.as_uint())
37            }
38        }
39
40        impl From<$scalar> for $crate::ScalarValue<$curve> {
41            fn from(scalar: $scalar) -> $crate::ScalarValue<$curve> {
42                $crate::ScalarValue::from(&scalar)
43            }
44        }
45
46        impl From<&$scalar> for $crate::ScalarValue<$curve> {
47            fn from(scalar: &$scalar) -> $crate::ScalarValue<$curve> {
48                $crate::ScalarValue::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
59        #[doc = concat!("[`", stringify!(elliptic_curve), "::NonZeroScalar<", stringify!($curve), ">::new()`].")]
60        impl TryFrom<$scalar> for $crate::NonZeroScalar<$curve> {
61            type Error = $crate::Error;
62
63            fn try_from(scalar: $scalar) -> $crate::Result<Self> {
64                $crate::NonZeroScalar::new(scalar)
65                    .into_option()
66                    .ok_or($crate::Error)
67            }
68        }
69    };
70}
71
72/// Writes a series of `Mul` impls for an elliptic curve's scalar field.
73#[macro_export]
74macro_rules! scalar_mul_impls {
75    ($curve:path, $scalar:ty) => {
76        impl ::core::ops::Mul<$crate::AffinePoint<$curve>> for $scalar {
77            type Output = $crate::ProjectivePoint<$curve>;
78
79            #[inline]
80            fn mul(self, rhs: $crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
81                rhs * self
82            }
83        }
84
85        impl ::core::ops::Mul<&$crate::AffinePoint<$curve>> for $scalar {
86            type Output = $crate::ProjectivePoint<$curve>;
87
88            #[inline]
89            fn mul(self, rhs: &$crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
90                *rhs * self
91            }
92        }
93
94        impl ::core::ops::Mul<$crate::AffinePoint<$curve>> for &$scalar {
95            type Output = $crate::ProjectivePoint<$curve>;
96
97            #[inline]
98            fn mul(self, rhs: $crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
99                rhs * self
100            }
101        }
102
103        impl ::core::ops::Mul<&$crate::AffinePoint<$curve>> for &$scalar {
104            type Output = $crate::ProjectivePoint<$curve>;
105
106            #[inline]
107            fn mul(self, rhs: &$crate::AffinePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
108                *rhs * self
109            }
110        }
111
112        impl ::core::ops::Mul<$crate::ProjectivePoint<$curve>> for $scalar {
113            type Output = $crate::ProjectivePoint<$curve>;
114
115            #[inline]
116            fn mul(self, rhs: $crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
117                rhs * self
118            }
119        }
120
121        impl ::core::ops::Mul<&$crate::ProjectivePoint<$curve>> for $scalar {
122            type Output = $crate::ProjectivePoint<$curve>;
123
124            #[inline]
125            fn mul(self, rhs: &$crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
126                rhs * &self
127            }
128        }
129
130        impl ::core::ops::Mul<$crate::ProjectivePoint<$curve>> for &$scalar {
131            type Output = $crate::ProjectivePoint<$curve>;
132
133            #[inline]
134            fn mul(self, rhs: $crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
135                rhs * self
136            }
137        }
138
139        impl ::core::ops::Mul<&$crate::ProjectivePoint<$curve>> for &$scalar {
140            type Output = $crate::ProjectivePoint<$curve>;
141
142            #[inline]
143            fn mul(self, rhs: &$crate::ProjectivePoint<$curve>) -> $crate::ProjectivePoint<$curve> {
144                rhs * self
145            }
146        }
147
148        impl $crate::ops::MulVartime<$crate::AffinePoint<$curve>> for $scalar {
149            #[inline]
150            fn mul_vartime(
151                self,
152                rhs: $crate::AffinePoint<$curve>,
153            ) -> $crate::ProjectivePoint<$curve> {
154                $crate::ops::MulVartime::mul_vartime(rhs, self)
155            }
156        }
157
158        impl $crate::ops::MulVartime<&$crate::AffinePoint<$curve>> for $scalar {
159            #[inline]
160            fn mul_vartime(
161                self,
162                rhs: &$crate::AffinePoint<$curve>,
163            ) -> $crate::ProjectivePoint<$curve> {
164                $crate::ops::MulVartime::mul_vartime(*rhs, &self)
165            }
166        }
167
168        impl $crate::ops::MulVartime<$crate::AffinePoint<$curve>> for &$scalar {
169            #[inline]
170            fn mul_vartime(
171                self,
172                rhs: $crate::AffinePoint<$curve>,
173            ) -> $crate::ProjectivePoint<$curve> {
174                $crate::ops::MulVartime::mul_vartime(rhs, self)
175            }
176        }
177
178        impl $crate::ops::MulVartime<&$crate::AffinePoint<$curve>> for &$scalar {
179            #[inline]
180            fn mul_vartime(
181                self,
182                rhs: &$crate::AffinePoint<$curve>,
183            ) -> $crate::ProjectivePoint<$curve> {
184                $crate::ops::MulVartime::mul_vartime(*rhs, self)
185            }
186        }
187
188        impl $crate::ops::MulVartime<$crate::ProjectivePoint<$curve>> for $scalar {
189            #[inline]
190            fn mul_vartime(
191                self,
192                rhs: $crate::ProjectivePoint<$curve>,
193            ) -> $crate::ProjectivePoint<$curve> {
194                $crate::ops::MulVartime::mul_vartime(rhs, self)
195            }
196        }
197
198        impl $crate::ops::MulVartime<&$crate::ProjectivePoint<$curve>> for $scalar {
199            #[inline]
200            fn mul_vartime(
201                self,
202                rhs: &$crate::ProjectivePoint<$curve>,
203            ) -> $crate::ProjectivePoint<$curve> {
204                $crate::ops::MulVartime::mul_vartime(*rhs, &self)
205            }
206        }
207
208        impl $crate::ops::MulVartime<$crate::ProjectivePoint<$curve>> for &$scalar {
209            #[inline]
210            fn mul_vartime(
211                self,
212                rhs: $crate::ProjectivePoint<$curve>,
213            ) -> $crate::ProjectivePoint<$curve> {
214                $crate::ops::MulVartime::mul_vartime(rhs, self)
215            }
216        }
217
218        impl $crate::ops::MulVartime<&$crate::ProjectivePoint<$curve>> for &$scalar {
219            #[inline]
220            fn mul_vartime(
221                self,
222                rhs: &$crate::ProjectivePoint<$curve>,
223            ) -> $crate::ProjectivePoint<$curve> {
224                $crate::ops::MulVartime::mul_vartime(*rhs, self)
225            }
226        }
227    };
228}