Define mul variants for MontgomeryPoints.

This results in less changes to the x25519-dalek code to upgrade to
the latest version.
This commit is contained in:
Isis Lovecruft 2018-04-02 21:09:41 +00:00
parent 1a8cf7beed
commit 3281708965
Failed to extract signature

View file

@ -229,12 +229,17 @@ fn differential_add_and_double(
Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2 Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2
} }
define_mul_assign_variants!(LHS = MontgomeryPoint, RHS = Scalar);
define_mul_variants!(LHS = MontgomeryPoint, RHS = Scalar, Output = MontgomeryPoint);
define_mul_variants!(LHS = Scalar, RHS = MontgomeryPoint, Output = MontgomeryPoint);
/// Multiply this `MontgomeryPoint` by a `Scalar`. /// Multiply this `MontgomeryPoint` by a `Scalar`.
impl Mul<Scalar> for MontgomeryPoint { impl<'a, 'b> Mul<&'b Scalar> for &'a MontgomeryPoint {
type Output = MontgomeryPoint; type Output = MontgomeryPoint;
/// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0([n]P) \\). /// Given `self` \\( = u\_0(P) \\), and a `Scalar` \\(n\\), return \\( u\_0([n]P) \\).
fn mul(self, scalar: Scalar) -> MontgomeryPoint { fn mul(self, scalar: &'b Scalar) -> MontgomeryPoint {
// Algorithm 8 of Costello-Smith 2017 // Algorithm 8 of Costello-Smith 2017
let affine_u = FieldElement::from_bytes(&self.0); let affine_u = FieldElement::from_bytes(&self.0);
let mut x0 = ProjectivePoint::identity(); let mut x0 = ProjectivePoint::identity();
@ -256,16 +261,16 @@ impl Mul<Scalar> for MontgomeryPoint {
} }
} }
impl MulAssign<Scalar> for MontgomeryPoint { impl<'b> MulAssign<&'b Scalar> for MontgomeryPoint {
fn mul_assign(&mut self, scalar: Scalar) { fn mul_assign(&mut self, scalar: &'b Scalar) {
*self = (*self) * scalar; *self = (self as &MontgomeryPoint) * scalar;
} }
} }
impl Mul<MontgomeryPoint> for Scalar { impl<'a, 'b> Mul<&'b MontgomeryPoint> for &'a Scalar {
type Output = MontgomeryPoint; type Output = MontgomeryPoint;
fn mul(self, point: MontgomeryPoint) -> MontgomeryPoint { fn mul(self, point: &'b MontgomeryPoint) -> MontgomeryPoint {
point * self point * self
} }
} }