Add wrapper for basepoint precomputations for decaf

This commit is contained in:
Henry & Isis 2017-03-10 22:43:07 -08:00 committed by Henry de Valence
parent c873f725a5
commit 049556147f
2 changed files with 35 additions and 1 deletions

View file

@ -875,7 +875,7 @@ impl EdwardsBasepointTable {
/// We then use the `select_precomputed_point` function, which /// We then use the `select_precomputed_point` function, which
/// takes `-8 ≤ x < 8` and `[16^2i * B, ..., 8 * 16^2i * B]`, /// takes `-8 ≤ x < 8` and `[16^2i * B, ..., 8 * 16^2i * B]`,
/// and returns `x * 16^2i * B` in constant time. /// 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 e = scalar.to_radix_16();
let mut h = ExtendedPoint::identity(); let mut h = ExtendedPoint::identity();
let mut t: CompletedPoint; let mut t: CompletedPoint;

View file

@ -31,7 +31,11 @@ use subtle::CTNegatable;
use core::ops::{Add, Sub, Neg}; use core::ops::{Add, Sub, Neg};
#[cfg(feature = "std")]
use std::boxed::Box;
use curve::ExtendedPoint; use curve::ExtendedPoint;
use curve::EdwardsBasepointTable;
use curve::BasepointMult; use curve::BasepointMult;
use curve::ScalarMult; use curve::ScalarMult;
use curve::Identity; use curve::Identity;
@ -261,6 +265,24 @@ impl BasepointMult<Scalar> 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<DecafBasepointTable> {
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 // Debug traits
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -356,6 +378,18 @@ mod test {
assert_eq!(P, Q); 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)] #[cfg(test)]