Skip to content

Commit 45489da

Browse files
authored
elliptic-curve: add MulByGeneratorVartime trait (#2381)
Closes #2375 We now have variable-time precomputed basepoint tables that use wNAF when the `basepoint-table` and `alloc` features are enabled, which can be opportunistically used when these features are enabled to accelerate this operation. We use `Group::mul_by_generator` for the constant-time basepoint tables, however for an extension trait this is captured as `MulByGeneratorVartime::mul_by_generator_vartime`. As discussed in the above issue, and inspired by `curve25519-dalek`'s `EdwardsPoint::vartime_double_scalar_mul_basepoint` function, this adds `MulByGeneratorVartime::mul_by_generator_and_mul_add_point_vartime` as a provided method. This function is the core of many signature algorithms, and when the basepoint tables and alloc are unavailable it can fall back to a linear combination and still provide better performance than the naive constant time version.
1 parent b48dd1e commit 45489da

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

elliptic-curve/src/ops.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Shr, ShrAssign, Sub, Su
66
use crate::CurveGroup;
77
use core::iter;
88
use ff::Field;
9+
use group::Group;
910
use subtle::{Choice, CtOption};
1011

1112
#[cfg(feature = "alloc")]
@@ -198,6 +199,35 @@ pub trait MulVartime<Rhs = Self>: Mul<Rhs> {
198199
fn mul_vartime(self, rhs: Rhs) -> <Self as Mul<Rhs>>::Output;
199200
}
200201

202+
/// Variable-time multiplication by the generator of the curve group.
203+
///
204+
/// <div class="warning">
205+
/// <b>Security Warning</b>
206+
///
207+
/// Variable-time operations should only be used on non-secret values, and may potentially leak
208+
/// secret values!
209+
/// </div>
210+
pub trait MulByGeneratorVartime: Group + for<'a> MulVartime<&'a Self::Scalar> {
211+
/// Multiply by the generator of the prime-order subgroup.
212+
///
213+
/// Variable-time equivalent of [`Group::mul_by_generator`].
214+
fn mul_by_generator_vartime(scalar: &Self::Scalar) -> Self {
215+
Self::generator().mul_vartime(scalar)
216+
}
217+
218+
/// Multiply `a` by the generator of the prime-order subgroup, adding the result to the point
219+
/// `B` multiplied by the scalar `b`, i.e. compute `aG + bB`.
220+
///
221+
/// This operation is the core of many signature verification algorithms.
222+
fn mul_by_generator_and_mul_add_point_vartime(
223+
a: &Self::Scalar,
224+
b_scalar: &Self::Scalar,
225+
b_point: &Self,
226+
) -> Self {
227+
Self::mul_by_generator_vartime(a) + b_point.mul_vartime(b_scalar)
228+
}
229+
}
230+
201231
/// Modular reduction to a non-zero output.
202232
///
203233
/// This trait is primarily intended for use by curve implementations such

0 commit comments

Comments
 (0)