From 7ef6a1e6fa0a3a94685c3c216454cb09cd093999 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Mon, 26 Mar 2018 17:35:08 -0700 Subject: [PATCH] Reorganize AVX2 point code --- src/backend/avx2/edwards.rs | 128 +++++++++--------- src/backend/avx2/scalar_mul/straus.rs | 5 +- src/backend/avx2/scalar_mul/variable_base.rs | 4 +- .../avx2/scalar_mul/vartime_double_base.rs | 3 +- src/backend/avx2/scalar_mul/vartime_straus.rs | 5 +- 5 files changed, 68 insertions(+), 77 deletions(-) diff --git a/src/backend/avx2/edwards.rs b/src/backend/avx2/edwards.rs index 6fa72bb..c9da1ea 100644 --- a/src/backend/avx2/edwards.rs +++ b/src/backend/avx2/edwards.rs @@ -24,7 +24,7 @@ use subtle::Choice; use edwards; use scalar::Scalar; -use scalar_mul::window::LookupTable; +use scalar_mul::window::{LookupTable, OddLookupTable}; use traits::Identity; @@ -76,62 +76,6 @@ impl Identity for ExtendedPoint { } } -/// A cached point with some precomputed variables used for readdition. -#[derive(Copy, Clone, Debug)] -pub struct CachedPoint(pub(super) FieldElement32x4); - -impl From for CachedPoint { - fn from(P: ExtendedPoint) -> CachedPoint { - let mut x = P.0; - - // x = (S2 S3 Z2 T2) - x.diff_sum(0b00001111); - - // x = (121666*S2 121666*S3 2*121666*Z2 2*121665*T2) - x.scale_by_curve_constants(); - - // x = (121666*S2 121666*S3 2*121666*Z2 -2*121665*T2) - x.negate(D_LANES); - - CachedPoint(x) - } -} - -impl Default for CachedPoint { - fn default() -> CachedPoint { - CachedPoint::identity() - } -} - -impl Identity for CachedPoint { - fn identity() -> CachedPoint { - CachedPoint(FieldElement32x4([ - u32x8::new(121647, 121666, 0, 0, 243332, 67108845, 0, 33554431), - u32x8::new(67108864, 0, 33554431, 0, 0, 67108863, 0, 33554431), - u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), - u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), - u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), - ])) - } -} - -impl ConditionallyAssignable for CachedPoint { - fn conditional_assign(&mut self, other: &CachedPoint, choice: Choice) { - self.0.conditional_assign(&other.0, choice); - } -} - -impl<'a> Neg for &'a CachedPoint { - type Output = CachedPoint; - - fn neg(self) -> CachedPoint { - let mut neg = *self; - neg.0.swap_AB(); - neg.0.negate_lazy(D_LANES); - neg - } -} - impl ExtendedPoint { pub fn double(&self) -> ExtendedPoint { unsafe { @@ -236,6 +180,62 @@ impl ExtendedPoint { } } +/// A cached point with some precomputed variables used for readdition. +#[derive(Copy, Clone, Debug)] +pub struct CachedPoint(pub(super) FieldElement32x4); + +impl From for CachedPoint { + fn from(P: ExtendedPoint) -> CachedPoint { + let mut x = P.0; + + // x = (S2 S3 Z2 T2) + x.diff_sum(0b00001111); + + // x = (121666*S2 121666*S3 2*121666*Z2 2*121665*T2) + x.scale_by_curve_constants(); + + // x = (121666*S2 121666*S3 2*121666*Z2 -2*121665*T2) + x.negate(D_LANES); + + CachedPoint(x) + } +} + +impl Default for CachedPoint { + fn default() -> CachedPoint { + CachedPoint::identity() + } +} + +impl Identity for CachedPoint { + fn identity() -> CachedPoint { + CachedPoint(FieldElement32x4([ + u32x8::new(121647, 121666, 0, 0, 243332, 67108845, 0, 33554431), + u32x8::new(67108864, 0, 33554431, 0, 0, 67108863, 0, 33554431), + u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), + u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), + u32x8::new(67108863, 0, 33554431, 0, 0, 67108863, 0, 33554431), + ])) + } +} + +impl ConditionallyAssignable for CachedPoint { + fn conditional_assign(&mut self, other: &CachedPoint, choice: Choice) { + self.0.conditional_assign(&other.0, choice); + } +} + +impl<'a> Neg for &'a CachedPoint { + type Output = CachedPoint; + + fn neg(self) -> CachedPoint { + let mut neg = *self; + neg.0.swap_AB(); + neg.0.negate_lazy(D_LANES); + neg + } +} + impl<'a, 'b> Add<&'b CachedPoint> for &'a ExtendedPoint { type Output = ExtendedPoint; @@ -288,8 +288,9 @@ impl<'a, 'b> Sub<&'b CachedPoint> for &'a ExtendedPoint { } } -impl From for LookupTable { - fn from(P: ExtendedPoint) -> Self { +impl<'a> From<&'a edwards::EdwardsPoint> for LookupTable { + fn from(point: &'a edwards::EdwardsPoint) -> Self { + let P = ExtendedPoint::from(*point); let mut points = [CachedPoint::from(P); 8]; for i in 0..7 { points[i+1] = (&P + &points[i]).into(); @@ -298,11 +299,10 @@ impl From for LookupTable { } } -use scalar_mul::window::OddLookupTable; - -impl<'a> From<&'a ExtendedPoint> for OddLookupTable { - fn from(A: &'a ExtendedPoint) -> Self { - let mut Ai = [CachedPoint::from(*A); 8]; +impl<'a> From<&'a edwards::EdwardsPoint> for OddLookupTable { + fn from(point: &'a edwards::EdwardsPoint) -> Self { + let A = ExtendedPoint::from(*point); + let mut Ai = [CachedPoint::from(A); 8]; let A2 = A.double(); for i in 0..7 { Ai[i + 1] = (&A2 + &Ai[i]).into(); diff --git a/src/backend/avx2/scalar_mul/straus.rs b/src/backend/avx2/scalar_mul/straus.rs index 5f76abe..59e123e 100644 --- a/src/backend/avx2/scalar_mul/straus.rs +++ b/src/backend/avx2/scalar_mul/straus.rs @@ -31,10 +31,7 @@ where // for each input point P let lookup_tables: Vec<_> = points .into_iter() - .map(|point| { - let avx2_point = ExtendedPoint::from(*point.borrow()); - LookupTable::::from(avx2_point) - }) + .map(|point| LookupTable::::from(point.borrow())) .collect(); let scalar_digits_vec: Vec<_> = scalars diff --git a/src/backend/avx2/scalar_mul/variable_base.rs b/src/backend/avx2/scalar_mul/variable_base.rs index e5261fb..6f36bc7 100644 --- a/src/backend/avx2/scalar_mul/variable_base.rs +++ b/src/backend/avx2/scalar_mul/variable_base.rs @@ -8,10 +8,8 @@ use scalar_mul::window::LookupTable; /// Perform constant-time, variable-base scalar multiplication. pub fn mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint { - // XXX combine these conversions - let avx2_point = ExtendedPoint::from(*point); // Construct a lookup table of [P,2P,3P,4P,5P,6P,7P,8P] - let lookup_table = LookupTable::::from(avx2_point); + let lookup_table = LookupTable::::from(point); // Setting s = scalar, compute // // s = s_0 + s_1*16^1 + ... + s_63*16^63, diff --git a/src/backend/avx2/scalar_mul/vartime_double_base.rs b/src/backend/avx2/scalar_mul/vartime_double_base.rs index f7703bb..1ba09f1 100644 --- a/src/backend/avx2/scalar_mul/vartime_double_base.rs +++ b/src/backend/avx2/scalar_mul/vartime_double_base.rs @@ -30,8 +30,7 @@ pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint { } } - let avx2_A = ExtendedPoint::from(*A); - let table_A = OddLookupTable::::from(&avx2_A); + let table_A = OddLookupTable::::from(A); let table_B = &BASEPOINT_ODD_LOOKUP_TABLE; let mut Q = ExtendedPoint::identity(); diff --git a/src/backend/avx2/scalar_mul/vartime_straus.rs b/src/backend/avx2/scalar_mul/vartime_straus.rs index c53e7d4..224713d 100644 --- a/src/backend/avx2/scalar_mul/vartime_straus.rs +++ b/src/backend/avx2/scalar_mul/vartime_straus.rs @@ -31,10 +31,7 @@ where .collect(); let lookup_tables: Vec<_> = points .into_iter() - .map(|point| { - let avx2_point = ExtendedPoint::from(*point.borrow()); - OddLookupTable::::from(&avx2_point) - }) + .map(|point| OddLookupTable::::from(point.borrow())) .collect(); let mut Q = ExtendedPoint::identity();