Generalize k_fold_scalar_mult

This commit is contained in:
Henry de Valence 2017-05-04 00:02:29 -07:00
parent c6dc9d318d
commit c18627f7c2
2 changed files with 17 additions and 12 deletions

View file

@ -1066,12 +1066,15 @@ pub mod vartime {
/// ///
/// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an /// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an
/// error to call this function with two vectors of different lengths. /// error to call this function with two vectors of different lengths.
pub fn k_fold_scalar_mult(scalars: &Vec<Scalar>, pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> ExtendedPoint
points: &Vec<ExtendedPoint>) -> ExtendedPoint { where I: IntoIterator<Item=&'a Scalar>, J: IntoIterator<Item=&'b ExtendedPoint>
assert_eq!(scalars.len(), points.len()); {
//assert_eq!(scalars.len(), points.len());
let nafs: Vec<_> = scalars.iter().map(|c| c.non_adjacent_form()).collect(); let nafs: Vec<_> = scalars.into_iter()
let odd_multiples: Vec<_> = points.iter().map(|P| OddMultiples::create(&P)).collect(); .map(|c| c.non_adjacent_form()).collect();
let odd_multiples: Vec<_> = points.into_iter()
.map(|P| OddMultiples::create(P)).collect();
let mut r = ProjectivePoint::identity(); let mut r = ProjectivePoint::identity();
@ -1471,9 +1474,10 @@ mod test {
#[test] #[test]
fn k_fold_scalar_mult_vs_ed25519py() { fn k_fold_scalar_mult_vs_ed25519py() {
let A = A_TIMES_BASEPOINT.decompress().unwrap(); let A = A_TIMES_BASEPOINT.decompress().unwrap();
let points = vec![A,constants::ED25519_BASEPOINT]; let result = vartime::k_fold_scalar_mult(
let scalars = vec![A_SCALAR, B_SCALAR]; &[A_SCALAR, B_SCALAR],
let result = vartime::k_fold_scalar_mult(&scalars, &points); &[A, constants::ED25519_BASEPOINT]
);
assert_eq!(result.compress_edwards(), DOUBLE_SCALAR_MULT_RESULT); assert_eq!(result.compress_edwards(), DOUBLE_SCALAR_MULT_RESULT);
} }
} }

View file

@ -318,10 +318,11 @@ pub mod vartime {
/// ///
/// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an /// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an
/// error to call this function with two vectors of different lengths. /// error to call this function with two vectors of different lengths.
pub fn k_fold_scalar_mult(scalars: &Vec<Scalar>, pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> DecafPoint
points: &Vec<DecafPoint>) -> DecafPoint { where I: IntoIterator<Item=&'a Scalar>, J: IntoIterator<Item=&'b DecafPoint>
let extended_points: Vec<ExtendedPoint> = points.iter().map(|P| P.0).collect(); {
DecafPoint(curve::vartime::k_fold_scalar_mult(scalars, &extended_points)) let extended_points = points.into_iter().map(|P| &P.0);
DecafPoint(curve::vartime::k_fold_scalar_mult(scalars, extended_points))
} }
} }