From 049556147fcc83de20ed0a071d9f90022dc7246c Mon Sep 17 00:00:00 2001 From: Henry & Isis Date: Fri, 10 Mar 2017 22:43:07 -0800 Subject: [PATCH] Add wrapper for basepoint precomputations for decaf --- src/curve.rs | 2 +- src/decaf.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/curve.rs b/src/curve.rs index b736f65..f90e69b 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -875,7 +875,7 @@ impl EdwardsBasepointTable { /// We then use the `select_precomputed_point` function, which /// takes `-8 ≤ x < 8` and `[16^2i * B, ..., 8 * 16^2i * B]`, /// and returns `x * 16^2i * B` in constant time. - fn basepoint_mult(&self, scalar: &Scalar) -> ExtendedPoint { //GeScalarMultBase + pub fn basepoint_mult(&self, scalar: &Scalar) -> ExtendedPoint { let e = scalar.to_radix_16(); let mut h = ExtendedPoint::identity(); let mut t: CompletedPoint; diff --git a/src/decaf.rs b/src/decaf.rs index 99fe115..71a8d1c 100644 --- a/src/decaf.rs +++ b/src/decaf.rs @@ -31,7 +31,11 @@ use subtle::CTNegatable; use core::ops::{Add, Sub, Neg}; +#[cfg(feature = "std")] +use std::boxed::Box; + use curve::ExtendedPoint; +use curve::EdwardsBasepointTable; use curve::BasepointMult; use curve::ScalarMult; use curve::Identity; @@ -261,6 +265,24 @@ impl BasepointMult for DecafPoint { } } + +/// Precomputation +#[derive(Clone)] +pub struct DecafBasepointTable(EdwardsBasepointTable); + +impl DecafBasepointTable { + /// Create a precomputed table of multiples of the given `basepoint`. + pub fn create(basepoint: &DecafPoint) -> Box { + let edwards_table = EdwardsBasepointTable::create(&basepoint.0); + box DecafBasepointTable(*edwards_table) + } + + /// Use the precomputed table to quickly compute `scalar * basepoint` + pub fn basepoint_mult(&self, scalar: &Scalar) -> DecafPoint { + DecafPoint(self.0.basepoint_mult(scalar)) + } +} + // ------------------------------------------------------------------------ // Debug traits // ------------------------------------------------------------------------ @@ -356,6 +378,18 @@ mod test { assert_eq!(P, Q); } } + + /// Test basepoint_mult versus a newly-generated DecafBasepointTable + #[test] + fn basepoint_mult_vs_decafbasepointtable() { + let table = DecafBasepointTable::create(&DecafPoint::basepoint()); + let mut rng = OsRng::new().unwrap(); + let s = Scalar::random(&mut rng); + let basepoint_mult_s = DecafPoint::basepoint_mult(&s); + let table_basepoint_mult_s = table.basepoint_mult(&s); + + assert_eq!(basepoint_mult_s, table_basepoint_mult_s); + } } #[cfg(test)]