From 6cec313f16a03f8125148ebdd7873f14267f6cab Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 23 Nov 2018 18:35:08 -0800 Subject: [PATCH] Add impl of Sub and Neg for Edwards points. --- src/backend/vector/ifma/edwards.rs | 28 +++++++++++++++++++++++----- src/backend/vector/ifma/field.rs | 10 ++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/backend/vector/ifma/edwards.rs b/src/backend/vector/ifma/edwards.rs index 9c2e7e8..1a16d42 100644 --- a/src/backend/vector/ifma/edwards.rs +++ b/src/backend/vector/ifma/edwards.rs @@ -9,7 +9,7 @@ use traits::Identity; -use std::ops::Add; +use std::ops::{Add, Neg, Sub}; use edwards; @@ -132,6 +132,24 @@ impl<'a, 'b> Add<&'b CachedPoint> for &'a ExtendedPoint { } } +impl<'a> Neg for &'a CachedPoint { + type Output = CachedPoint; + + fn neg(self) -> CachedPoint { + let swapped = self.0.shuffle(Shuffle::BACD); + CachedPoint(swapped.blend(&(-self.0), Lanes::D)) + } +} + +impl<'a, 'b> Sub<&'b CachedPoint> for &'a ExtendedPoint { + type Output = ExtendedPoint; + + /// Implement subtraction by negating the point and adding. + fn sub(self, other: &'b CachedPoint) -> ExtendedPoint { + self + &(-other) + } +} + #[cfg(test)] mod test { use super::*; @@ -143,7 +161,7 @@ mod test { // Test the vector implementation of the parallel readdition formulas let cached_Q = CachedPoint::from(ExtendedPoint::from(Q)); let R_vector: edwards::EdwardsPoint = (&ExtendedPoint::from(P) + &cached_Q).into(); - //et S_vector: edwards::EdwardsPoint = (&ExtendedPoint::from(P) - &cached_Q).into(); + let S_vector: edwards::EdwardsPoint = (&ExtendedPoint::from(P) - &cached_Q).into(); println!("Testing point addition:"); println!("P = {:?}", P); @@ -152,11 +170,11 @@ mod test { println!("R = P + Q = {:?}", &P + &Q); //println!("R_serial = {:?}", R_serial); println!("R_vector = {:?}", R_vector); - //println!("S = P - Q = {:?}", &P - &Q); - //println!("S_vector = {:?}", S_vector); + println!("S = P - Q = {:?}", &P - &Q); + println!("S_vector = {:?}", S_vector); //assert_eq!(R_serial.compress(), (&P + &Q).compress()); assert_eq!(R_vector.compress(), (&P + &Q).compress()); - //assert_eq!(S_vector.compress(), (&P - &Q).compress()); + assert_eq!(S_vector.compress(), (&P - &Q).compress()); println!("OK!\n"); } diff --git a/src/backend/vector/ifma/field.rs b/src/backend/vector/ifma/field.rs index 40220c5..fdc9d61 100644 --- a/src/backend/vector/ifma/field.rs +++ b/src/backend/vector/ifma/field.rs @@ -33,6 +33,7 @@ pub enum Shuffle { AAAA, BBBB, BADC, + BACD, ADDA, CBCB, ABDC, @@ -50,6 +51,7 @@ fn shuffle_lanes(x: u64x4, control: Shuffle) -> u64x4 { Shuffle::AAAA => perm(x.into_bits(), 0b00_00_00_00).into_bits(), Shuffle::BBBB => perm(x.into_bits(), 0b01_01_01_01).into_bits(), Shuffle::BADC => perm(x.into_bits(), 0b10_11_00_01).into_bits(), + Shuffle::BACD => perm(x.into_bits(), 0b11_10_00_01).into_bits(), Shuffle::ADDA => perm(x.into_bits(), 0b00_11_11_00).into_bits(), Shuffle::CBCB => perm(x.into_bits(), 0b01_10_01_10).into_bits(), Shuffle::ABDC => perm(x.into_bits(), 0b10_11_01_00).into_bits(), @@ -186,6 +188,14 @@ impl F51x4Unreduced { } } +impl Neg for F51x4Reduced { + type Output = F51x4Reduced; + + fn neg(self) -> F51x4Reduced { + F51x4Unreduced::from(self).negate_lazy().into() + } +} + impl F51x4Reduced { #[inline] pub fn shuffle(&self, control: Shuffle) -> F51x4Reduced {