Skip to main content

primeorder/
mul_backend.rs

1//! Scalar multiplication backends.
2
3#[cfg(feature = "basepoint-table")]
4mod precomputed_tables;
5mod variable_only;
6
7use crate::{PrimeCurveParams, ProjectivePoint};
8use elliptic_curve::Scalar;
9use elliptic_curve::ops::LinearCombination;
10
11#[cfg(feature = "basepoint-table")]
12pub use self::precomputed_tables::PrecomputedTables;
13pub use self::variable_only::VariableOnly;
14
15/// Scalar multiplication backend.
16pub trait MulBackend<C: PrimeCurveParams> {
17    /// Multiplication by the generator.
18    ///
19    /// This is overridable to make it possible to plug in a basepoint table.
20    #[inline]
21    fn mul_by_generator(k: &Scalar<C>) -> ProjectivePoint<C> {
22        ProjectivePoint::GENERATOR * k
23    }
24
25    /// Variable-time multiplication by the generator.
26    ///
27    /// This is overridable to make it possible to plug in a basepoint table.
28    #[inline]
29    fn mul_by_generator_vartime(k: &Scalar<C>) -> ProjectivePoint<C> {
30        ProjectivePoint::GENERATOR.mul_vartime(k)
31    }
32
33    /// Multiply `a` by the generator of the prime-order subgroup, adding the result to the point
34    /// `P` multiplied by the scalar `b`, i.e. compute `aG + bP`.
35    #[inline]
36    fn mul_by_generator_and_mul_add_vartime(
37        a: &Scalar<C>,
38        b_scalar: &Scalar<C>,
39        b_point: &ProjectivePoint<C>,
40    ) -> ProjectivePoint<C> {
41        ProjectivePoint::<C>::lincomb_vartime(&[
42            (ProjectivePoint::GENERATOR, *a),
43            (*b_point, *b_scalar),
44        ])
45    }
46}