Merge pull request #121 from isislovecruft/feature/montgomery-mul-variants

Define mul variants for MontgomeryPoints.
This commit is contained in:
Henry de Valence 2018-04-03 10:06:48 -07:00 committed by GitHub
commit 2465e0f962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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
}
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`.
impl Mul<Scalar> for MontgomeryPoint {
impl<'a, 'b> Mul<&'b Scalar> for &'a MontgomeryPoint {
type Output = MontgomeryPoint;
/// 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
let affine_u = FieldElement::from_bytes(&self.0);
let mut x0 = ProjectivePoint::identity();
@ -256,16 +261,16 @@ impl Mul<Scalar> for MontgomeryPoint {
}
}
impl MulAssign<Scalar> for MontgomeryPoint {
fn mul_assign(&mut self, scalar: Scalar) {
*self = (*self) * scalar;
impl<'b> MulAssign<&'b Scalar> for MontgomeryPoint {
fn mul_assign(&mut self, scalar: &'b Scalar) {
*self = (self as &MontgomeryPoint) * scalar;
}
}
impl Mul<MontgomeryPoint> for Scalar {
impl<'a, 'b> Mul<&'b MontgomeryPoint> for &'a Scalar {
type Output = MontgomeryPoint;
fn mul(self, point: MontgomeryPoint) -> MontgomeryPoint {
fn mul(self, point: &'b MontgomeryPoint) -> MontgomeryPoint {
point * self
}
}