Skip to content

Commit b48dd1e

Browse files
authored
elliptic-curve: MulVartime fixups (#2380)
- Adds `MulVartime` to `AffinePoint` and `ProjectivePoint` bounds - Adds boilerplate `MulVartime` impls to `scalar_mul_impls!` - Uses `scalar_mul_impls!` for `MockCurve`
1 parent d4c0d46 commit b48dd1e

File tree

3 files changed

+135
-58
lines changed

3 files changed

+135
-58
lines changed

elliptic-curve/src/arithmetic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub trait CurveArithmetic: Curve {
2929
+ Eq
3030
+ From<NonIdentity<Self::AffinePoint>>
3131
+ Generate
32+
+ MulVartime<Self::Scalar>
33+
+ for<'a> MulVartime<&'a Self::Scalar>
3234
+ PartialEq
3335
+ Sized
3436
+ Send
@@ -58,6 +60,8 @@ pub trait CurveArithmetic: Curve {
5860
+ Into<Self::AffinePoint>
5961
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar)]>
6062
+ LinearCombination<[(Self::ProjectivePoint, Self::Scalar); 2]>
63+
+ MulVartime<Self::Scalar>
64+
+ for<'a> MulVartime<&'a Self::Scalar>
6165
+ TryInto<NonIdentity<Self::ProjectivePoint>, Error = Error>
6266
+ CurveGroup<AffineRepr = Self::AffinePoint>
6367
+ Group<Scalar = Self::Scalar>;

elliptic-curve/src/dev/mock_curve.rs

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,10 @@ impl SubAssign<&Scalar> for Scalar {
274274
}
275275
}
276276

277+
crate::scalar_mul_impls!(MockCurve, Scalar);
278+
277279
impl Mul<Scalar> for Scalar {
278280
type Output = Scalar;
279-
280281
fn mul(self, _other: Scalar) -> Scalar {
281282
unimplemented!();
282283
}
@@ -290,62 +291,6 @@ impl Mul<&Scalar> for Scalar {
290291
}
291292
}
292293

293-
impl Mul<AffinePoint> for Scalar {
294-
type Output = ProjectivePoint;
295-
296-
fn mul(self, _other: AffinePoint) -> ProjectivePoint {
297-
unimplemented!();
298-
}
299-
}
300-
301-
impl MulVartime<AffinePoint> for Scalar {
302-
fn mul_vartime(self, _other: AffinePoint) -> ProjectivePoint {
303-
unimplemented!();
304-
}
305-
}
306-
307-
impl Mul<&AffinePoint> for Scalar {
308-
type Output = ProjectivePoint;
309-
310-
fn mul(self, _other: &AffinePoint) -> ProjectivePoint {
311-
unimplemented!();
312-
}
313-
}
314-
315-
impl MulVartime<&AffinePoint> for Scalar {
316-
fn mul_vartime(self, _other: &AffinePoint) -> ProjectivePoint {
317-
unimplemented!();
318-
}
319-
}
320-
321-
impl Mul<ProjectivePoint> for Scalar {
322-
type Output = ProjectivePoint;
323-
324-
fn mul(self, _other: ProjectivePoint) -> ProjectivePoint {
325-
unimplemented!();
326-
}
327-
}
328-
329-
impl MulVartime<ProjectivePoint> for Scalar {
330-
fn mul_vartime(self, _other: ProjectivePoint) -> ProjectivePoint {
331-
unimplemented!();
332-
}
333-
}
334-
335-
impl Mul<&ProjectivePoint> for Scalar {
336-
type Output = ProjectivePoint;
337-
338-
fn mul(self, _other: &ProjectivePoint) -> ProjectivePoint {
339-
unimplemented!();
340-
}
341-
}
342-
343-
impl MulVartime<&ProjectivePoint> for Scalar {
344-
fn mul_vartime(self, _other: &ProjectivePoint) -> ProjectivePoint {
345-
unimplemented!();
346-
}
347-
}
348-
349294
impl MulAssign<Scalar> for Scalar {
350295
fn mul_assign(&mut self, _rhs: Scalar) {
351296
unimplemented!();
@@ -622,6 +567,34 @@ impl ToSec1Point<MockCurve> for AffinePoint {
622567
}
623568
}
624569

570+
impl Mul<Scalar> for AffinePoint {
571+
type Output = ProjectivePoint;
572+
573+
fn mul(self, _scalar: Scalar) -> ProjectivePoint {
574+
unimplemented!();
575+
}
576+
}
577+
578+
impl Mul<&Scalar> for AffinePoint {
579+
type Output = ProjectivePoint;
580+
581+
fn mul(self, _scalar: &Scalar) -> ProjectivePoint {
582+
unimplemented!();
583+
}
584+
}
585+
586+
impl MulVartime<Scalar> for AffinePoint {
587+
fn mul_vartime(self, _scalar: Scalar) -> ProjectivePoint {
588+
unimplemented!()
589+
}
590+
}
591+
592+
impl MulVartime<&Scalar> for AffinePoint {
593+
fn mul_vartime(self, _scalar: &Scalar) -> ProjectivePoint {
594+
unimplemented!()
595+
}
596+
}
597+
625598
impl Mul<NonZeroScalar> for AffinePoint {
626599
type Output = AffinePoint;
627600

@@ -982,6 +955,26 @@ impl Mul<&Scalar> for ProjectivePoint {
982955
}
983956
}
984957

958+
impl Mul<&Scalar> for &ProjectivePoint {
959+
type Output = ProjectivePoint;
960+
961+
fn mul(self, _scalar: &Scalar) -> ProjectivePoint {
962+
unimplemented!();
963+
}
964+
}
965+
966+
impl MulVartime<Scalar> for ProjectivePoint {
967+
fn mul_vartime(self, _scalar: Scalar) -> ProjectivePoint {
968+
unimplemented!()
969+
}
970+
}
971+
972+
impl MulVartime<&Scalar> for ProjectivePoint {
973+
fn mul_vartime(self, _scalar: &Scalar) -> ProjectivePoint {
974+
unimplemented!()
975+
}
976+
}
977+
985978
impl MulAssign<Scalar> for ProjectivePoint {
986979
fn mul_assign(&mut self, _rhs: Scalar) {
987980
unimplemented!();

elliptic-curve/src/macros.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ macro_rules! scalar_from_impls {
6969
};
7070
}
7171

72-
/// Writes a series of `Mul` impls for an elliptic curve's scalar field
72+
/// Writes a series of `Mul` impls for an elliptic curve's scalar field.
7373
#[macro_export]
7474
macro_rules! scalar_mul_impls {
7575
($curve:path, $scalar:ty) => {
@@ -144,5 +144,85 @@ macro_rules! scalar_mul_impls {
144144
rhs * self
145145
}
146146
}
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+
}
147227
};
148228
}

0 commit comments

Comments
 (0)