Skip to content

Commit d4c0d46

Browse files
authored
elliptic-curve: add ops::MulVartime trait and bound Scalar (#2379)
Adds a variable-time equivalent of the `Mul` trait with a corresponding `mul_vartime` method. This provides a place to plug in wNAF which is otherwise always available (and can fall back on constant-time operations if the `alloc` feature isn't enabled). The trait has been added to the bounds for `CurveArithmetic::Scalar`, with requirements to support variable-time multiplication for affine and projective points.
1 parent 4f084d3 commit d4c0d46

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

elliptic-curve/src/arithmetic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
Curve, CurveGroup, Error, FieldBytes, Group, NonZeroScalar, PrimeCurve, ScalarValue,
55
ctutils::{CtEq, CtSelect},
6-
ops::{Invert, LinearCombination, Mul, Reduce},
6+
ops::{Invert, LinearCombination, Mul, MulVartime, Reduce},
77
point::{AffineCoordinates, NonIdentity},
88
scalar::{FromUintUnchecked, IsHigh},
99
};
@@ -88,9 +88,13 @@ pub trait CurveArithmetic: Curve {
8888
+ Invert<Output = CtOption<Self::Scalar>>
8989
+ IsHigh
9090
+ Mul<Self::AffinePoint, Output = Self::ProjectivePoint>
91+
+ MulVartime<Self::AffinePoint>
9192
+ for<'a> Mul<&'a Self::AffinePoint, Output = Self::ProjectivePoint>
93+
+ for<'a> MulVartime<&'a Self::AffinePoint>
9294
+ Mul<Self::ProjectivePoint, Output = Self::ProjectivePoint>
95+
+ MulVartime<Self::ProjectivePoint>
9396
+ for<'a> Mul<&'a Self::ProjectivePoint, Output = Self::ProjectivePoint>
97+
+ for<'a> MulVartime<&'a Self::ProjectivePoint>
9498
+ PartialOrd
9599
+ Reduce<Self::Uint>
96100
+ Reduce<FieldBytes<Self>>

elliptic-curve/src/dev/mock_curve.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::{
1010
bigint::{Limb, Odd, U256, modular::Retrieve},
1111
ctutils,
1212
error::{Error, Result},
13-
ops::{Invert, LinearCombination, Reduce, ShrAssign},
13+
ops::{
14+
Add, AddAssign, Invert, LinearCombination, Mul, MulAssign, MulVartime, Neg, Reduce,
15+
ShrAssign, Sub, SubAssign,
16+
},
1417
point::{AffineCoordinates, NonIdentity},
1518
rand_core::{TryCryptoRng, TryRng},
1619
scalar::{FromUintUnchecked, IsHigh},
@@ -21,7 +24,6 @@ use crate::{
2124
use core::{
2225
array,
2326
iter::{Product, Sum},
24-
ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign},
2527
};
2628
use ff::{Field, PrimeField};
2729
use hex_literal::hex;
@@ -296,6 +298,12 @@ impl Mul<AffinePoint> for Scalar {
296298
}
297299
}
298300

301+
impl MulVartime<AffinePoint> for Scalar {
302+
fn mul_vartime(self, _other: AffinePoint) -> ProjectivePoint {
303+
unimplemented!();
304+
}
305+
}
306+
299307
impl Mul<&AffinePoint> for Scalar {
300308
type Output = ProjectivePoint;
301309

@@ -304,6 +312,12 @@ impl Mul<&AffinePoint> for Scalar {
304312
}
305313
}
306314

315+
impl MulVartime<&AffinePoint> for Scalar {
316+
fn mul_vartime(self, _other: &AffinePoint) -> ProjectivePoint {
317+
unimplemented!();
318+
}
319+
}
320+
307321
impl Mul<ProjectivePoint> for Scalar {
308322
type Output = ProjectivePoint;
309323

@@ -312,6 +326,12 @@ impl Mul<ProjectivePoint> for Scalar {
312326
}
313327
}
314328

329+
impl MulVartime<ProjectivePoint> for Scalar {
330+
fn mul_vartime(self, _other: ProjectivePoint) -> ProjectivePoint {
331+
unimplemented!();
332+
}
333+
}
334+
315335
impl Mul<&ProjectivePoint> for Scalar {
316336
type Output = ProjectivePoint;
317337

@@ -320,6 +340,12 @@ impl Mul<&ProjectivePoint> for Scalar {
320340
}
321341
}
322342

343+
impl MulVartime<&ProjectivePoint> for Scalar {
344+
fn mul_vartime(self, _other: &ProjectivePoint) -> ProjectivePoint {
345+
unimplemented!();
346+
}
347+
}
348+
323349
impl MulAssign<Scalar> for Scalar {
324350
fn mul_assign(&mut self, _rhs: Scalar) {
325351
unimplemented!();

elliptic-curve/src/ops.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ where
183183
}
184184
}
185185

186+
/// Variable-time equivalent of the [`Mul`] trait.
187+
///
188+
/// Should always compute the same results as [`Mul`], but may provide a faster implementation.
189+
///
190+
/// <div class="warning">
191+
/// <b>Security Warning</b>
192+
///
193+
/// Variable-time operations should only be used on non-secret values, and may potentially leak
194+
/// secret values!
195+
/// </div>
196+
pub trait MulVartime<Rhs = Self>: Mul<Rhs> {
197+
/// Multiply `self` by `rhs` in variable-time.
198+
fn mul_vartime(self, rhs: Rhs) -> <Self as Mul<Rhs>>::Output;
199+
}
200+
186201
/// Modular reduction to a non-zero output.
187202
///
188203
/// This trait is primarily intended for use by curve implementations such

0 commit comments

Comments
 (0)