From 2eed24109eff01ee974fbd1397170e13a939e669 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Tue, 15 May 2018 11:15:53 -0700 Subject: [PATCH] Move double-base scmul to the EdwardsPoint type --- benches/dalek_benchmarks.rs | 2 +- src/edwards.rs | 54 +++++++++++++++---------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/benches/dalek_benchmarks.rs b/benches/dalek_benchmarks.rs index 35d1a24..0248fdf 100644 --- a/benches/dalek_benchmarks.rs +++ b/benches/dalek_benchmarks.rs @@ -59,7 +59,7 @@ mod edwards_benches { let a = Scalar::from_u64(298374928).invert(); let b = Scalar::from_u64(897987897).invert(); let A = B * (b * a); - bench.iter(|| edwards::vartime::double_scalar_mul_basepoint(&a, &A, &b)); + bench.iter(|| EdwardsPoint::vartime_double_scalar_mul_basepoint(&a, &A, &b)); }); } diff --git a/src/edwards.rs b/src/edwards.rs index 36076cd..5f03929 100644 --- a/src/edwards.rs +++ b/src/edwards.rs @@ -584,6 +584,27 @@ impl VartimeMultiscalarMul for EdwardsPoint { } } +impl EdwardsPoint { + /// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint. + /// + /// XXX eliminate this function when we have the precomputation API + #[cfg(feature = "stage2_build")] + pub fn vartime_double_scalar_mul_basepoint(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint { + // If we built with AVX2, use the AVX2 backend. + #[cfg(all(feature="avx2_backend", target_feature="avx2"))] + { + use backend::avx2::scalar_mul::vartime_double_base::mul; + mul(a, A, b) + } + // Otherwise, proceed as normal: + #[cfg(not(all(feature="avx2_backend", target_feature="avx2")))] + { + use scalar_mul::vartime_double_base::mul; + mul(a, A, b) + } + } +} + /// A precomputed table of multiples of a basepoint, for accelerating /// fixed-base scalar multiplication. One table, for the Ed25519 /// basepoint, is provided in the `constants` module. @@ -783,37 +804,6 @@ impl Debug for EdwardsBasepointTable { } } -// ------------------------------------------------------------------------ -// Variable-time functions -// ------------------------------------------------------------------------ - -/// Variable-time operations on curve points, useful for non-secret data. -/// -/// XXX delete this whole module -pub mod vartime { - //! Variable-time operations on curve points, useful for non-secret data. - use super::*; - - /// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint. - /// - /// XXX eliminate this function when we have the precomputation API - #[cfg(feature = "stage2_build")] - pub fn double_scalar_mul_basepoint(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint { - // If we built with AVX2, use the AVX2 backend. - #[cfg(all(feature="avx2_backend", target_feature="avx2"))] - { - use backend::avx2::scalar_mul::vartime_double_base::mul; - mul(a, A, b) - } - // Otherwise, proceed as normal: - #[cfg(not(all(feature="avx2_backend", target_feature="avx2")))] - { - use scalar_mul::vartime_double_base::mul; - mul(a, A, b) - } - } -} - // ------------------------------------------------------------------------ // Tests // ------------------------------------------------------------------------ @@ -1143,7 +1133,7 @@ mod test { #[test] fn double_scalar_mul_basepoint_vs_ed25519py() { let A = A_TIMES_BASEPOINT.decompress().unwrap(); - let result = vartime::double_scalar_mul_basepoint(&A_SCALAR, &A, &B_SCALAR); + let result = EdwardsPoint::vartime_double_scalar_mul_basepoint(&A_SCALAR, &A, &B_SCALAR); assert_eq!(result.compress(), DOUBLE_SCALAR_MULT_RESULT); }