From e1193d2ae9f8266ea5424fda357a22a547e7053e Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 29 May 2019 17:30:24 +0100 Subject: [PATCH] impl Mul for [Extended|Affine]NielsPoint --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ src/util.rs | 22 ++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7825724..efee90e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -252,6 +252,16 @@ impl AffineNielsPoint { } } +impl<'a, 'b> Mul<&'b Fr> for &'a AffineNielsPoint { + type Output = ExtendedPoint; + + fn mul(self, other: &'b Fr) -> ExtendedPoint { + self.multiply(&other.into_bytes()) + } +} + +impl_binops_multiplicative_mixed!(AffineNielsPoint, Fr, ExtendedPoint); + impl ConditionallySelectable for AffineNielsPoint { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { AffineNielsPoint { @@ -326,6 +336,16 @@ impl ExtendedNielsPoint { } } +impl<'a, 'b> Mul<&'b Fr> for &'a ExtendedNielsPoint { + type Output = ExtendedPoint; + + fn mul(self, other: &'b Fr) -> ExtendedPoint { + self.multiply(&other.into_bytes()) + } +} + +impl_binops_multiplicative_mixed!(ExtendedNielsPoint, Fr, ExtendedPoint); + // `d = -(10240/10241)` const EDWARDS_D: Fq = Fq::from_raw([ 0x01065fd6d6343eb1, @@ -1198,6 +1218,17 @@ fn test_mul_consistency() { ]), }).mul_by_cofactor(); assert_eq!(p * c, (p * a) * b); + + // Test Mul implemented on ExtendedNielsPoint + assert_eq!(p * c, (p.to_niels() * a) * b); + assert_eq!(p.to_niels() * c, (p * a) * b); + assert_eq!(p.to_niels() * c, (p.to_niels() * a) * b); + + // Test Mul implemented on AffineNielsPoint + let p_affine_niels = AffinePoint::from(p).to_niels(); + assert_eq!(p * c, (p_affine_niels * a) * b); + assert_eq!(p_affine_niels * c, (p * a) * b); + assert_eq!(p_affine_niels * c, (p_affine_niels * a) * b); } #[test] diff --git a/src/util.rs b/src/util.rs index f45ab01..05e0e48 100644 --- a/src/util.rs +++ b/src/util.rs @@ -105,34 +105,40 @@ macro_rules! impl_binops_additive { }; } -macro_rules! impl_binops_multiplicative { - ($lhs:ident, $rhs:ident) => { +macro_rules! impl_binops_multiplicative_mixed { + ($lhs:ident, $rhs:ident, $output:ident) => { impl<'b> Mul<&'b $rhs> for $lhs { - type Output = $lhs; + type Output = $output; #[inline] - fn mul(self, rhs: &'b $rhs) -> $lhs { + fn mul(self, rhs: &'b $rhs) -> $output { &self * rhs } } impl<'a> Mul<$rhs> for &'a $lhs { - type Output = $lhs; + type Output = $output; #[inline] - fn mul(self, rhs: $rhs) -> $lhs { + fn mul(self, rhs: $rhs) -> $output { self * &rhs } } impl Mul<$rhs> for $lhs { - type Output = $lhs; + type Output = $output; #[inline] - fn mul(self, rhs: $rhs) -> $lhs { + fn mul(self, rhs: $rhs) -> $output { &self * &rhs } } + }; +} + +macro_rules! impl_binops_multiplicative { + ($lhs:ident, $rhs:ident) => { + impl_binops_multiplicative_mixed!($lhs, $rhs, $lhs); impl MulAssign<$rhs> for $lhs { #[inline]