rustfmt and copyright fixes

This commit is contained in:
Henry de Valence 2019-05-22 11:38:52 -07:00
parent 7fba2a1bcc
commit dfcac0d8e2
2 changed files with 35 additions and 38 deletions

View file

@ -1,12 +1,10 @@
// -*- mode: rust; -*- // -*- mode: rust; -*-
// //
// This file is part of curve25519-dalek. // This file is part of curve25519-dalek.
// Copyright (c) 2016-2018 Isis Lovecruft, Henry de Valence // Copyright (c) 2019 Oleg Andreev
// See LICENSE for licensing information. // See LICENSE for licensing information.
// //
// Authors: // Authors:
// - Isis Agora Lovecruft <isis@patternsinthevoid.net>
// - Henry de Valence <hdevalence@hdevalence.ca>
// - Oleg Andreev <oleganza@gmail.com> // - Oleg Andreev <oleganza@gmail.com>
//! Implementation of a variant of Pippenger's algorithm. //! Implementation of a variant of Pippenger's algorithm.
@ -72,8 +70,8 @@ impl VartimeMultiscalarMul for Pippenger {
I::Item: Borrow<Scalar>, I::Item: Borrow<Scalar>,
J: IntoIterator<Item = Option<EdwardsPoint>>, J: IntoIterator<Item = Option<EdwardsPoint>>,
{ {
use backend::serial::curve_models::{ProjectiveNielsPoint}; use backend::serial::curve_models::ProjectiveNielsPoint;
use traits::Identity; use traits::Identity;
let mut scalars = scalars.into_iter(); let mut scalars = scalars.into_iter();
let size = scalars.by_ref().size_hint().0; let size = scalars.by_ref().size_hint().0;
@ -95,13 +93,15 @@ impl VartimeMultiscalarMul for Pippenger {
// Collect optimized scalars and points in buffers for repeated access // Collect optimized scalars and points in buffers for repeated access
// (scanning the whole set per digit position). // (scanning the whole set per digit position).
let scalars = scalars.into_iter() let scalars = scalars
.map(|s| s.borrow().to_pippenger_radix(w).0 ) .into_iter()
.collect::<Vec<_>>(); .map(|s| s.borrow().to_pippenger_radix(w).0)
.collect::<Vec<_>>();
let points: Vec<ProjectiveNielsPoint> = match points let points: Vec<ProjectiveNielsPoint> = match points
.into_iter() .into_iter()
.map(|p| p.map(|P| P.to_projective_niels())) .map(|p| p.map(|P| P.to_projective_niels()))
.collect::<Option<Vec<_>>>() { .collect::<Option<Vec<_>>>()
{
Some(x) => x, Some(x) => x,
None => return None, None => return None,
}; };
@ -113,18 +113,17 @@ impl VartimeMultiscalarMul for Pippenger {
.collect(); .collect();
let mut columns = (0..digits_count).rev().map(|digit_index| { let mut columns = (0..digits_count).rev().map(|digit_index| {
// Clear the buckets when processing another digit. // Clear the buckets when processing another digit.
for i in 0..buckets_count { for i in 0..buckets_count {
buckets[i] = EdwardsPoint::identity(); buckets[i] = EdwardsPoint::identity();
} }
// Iterate over pairs of (point, scalar) // Iterate over pairs of (point, scalar)
// and add/sub the point to the corresponding bucket. // and add/sub the point to the corresponding bucket.
// Note: if we add support for precomputed lookup tables, // Note: if we add support for precomputed lookup tables,
// we'll be adding/subtractiong point premultiplied by `digits[i]` to buckets[0]. // we'll be adding/subtractiong point premultiplied by `digits[i]` to buckets[0].
for (digits, pt) in scalars.iter().zip(points.iter()) { for (digits, pt) in scalars.iter().zip(points.iter()) {
let digit = digits[digit_index]; let digit = digits[digit_index];
if digit > 0 { if digit > 0 {
let b = (digit - 1) as usize; let b = (digit - 1) as usize;
buckets[b] = (&buckets[b] + pt).to_extended(); buckets[b] = (&buckets[b] + pt).to_extended();
@ -157,9 +156,9 @@ impl VartimeMultiscalarMul for Pippenger {
let hi_column = columns.next().unwrap(); let hi_column = columns.next().unwrap();
Some( Some(
columns.fold(hi_column, |total, p| { columns
total.mul_by_pow_2(w as u32) + p .fold(hi_column, |total, p| total.mul_by_pow_2(w as u32) + p)
}).into() .into(),
) )
} }
} }
@ -177,12 +176,10 @@ mod test {
let x = Scalar::from(2128506u64).invert(); let x = Scalar::from(2128506u64).invert();
let y = Scalar::from(4443282u64).invert(); let y = Scalar::from(4443282u64).invert();
let points: Vec<_> = (0..n) let points: Vec<_> = (0..n)
.map(|i| { .map(|i| constants::ED25519_BASEPOINT_POINT * Scalar::from(1 + i as u64))
constants::ED25519_BASEPOINT_POINT * Scalar::from(1 + i as u64)
})
.collect(); .collect();
let scalars: Vec<_> = (0..n) let scalars: Vec<_> = (0..n)
.map(|i| x + (Scalar::from(i as u64)*y)) // fast way to make ~random but deterministic scalars .map(|i| x + (Scalar::from(i as u64) * y)) // fast way to make ~random but deterministic scalars
.collect(); .collect();
let premultiplied: Vec<EdwardsPoint> = scalars let premultiplied: Vec<EdwardsPoint> = scalars

View file

@ -1,12 +1,10 @@
// -*- mode: rust; -*- // -*- mode: rust; -*-
// //
// This file is part of curve25519-dalek. // This file is part of curve25519-dalek.
// Copyright (c) 2016-2018 Isis Lovecruft, Henry de Valence // Copyright (c) 2019 Oleg Andreev
// See LICENSE for licensing information. // See LICENSE for licensing information.
// //
// Authors: // Authors:
// - Isis Agora Lovecruft <isis@patternsinthevoid.net>
// - Henry de Valence <hdevalence@hdevalence.ca>
// - Oleg Andreev <oleganza@gmail.com> // - Oleg Andreev <oleganza@gmail.com>
#![allow(non_snake_case)] #![allow(non_snake_case)]
@ -52,13 +50,15 @@ impl VartimeMultiscalarMul for Pippenger {
// Collect optimized scalars and points in buffers for repeated access // Collect optimized scalars and points in buffers for repeated access
// (scanning the whole set per digit position). // (scanning the whole set per digit position).
let scalars = scalars.into_iter() let scalars = scalars
.map(|s| s.borrow().to_pippenger_radix(w).0 ) .into_iter()
.collect::<Vec<_>>(); .map(|s| s.borrow().to_pippenger_radix(w).0)
.collect::<Vec<_>>();
let points: Vec<CachedPoint> = match points let points: Vec<CachedPoint> = match points
.into_iter() .into_iter()
.map(|p| p.map(|P| CachedPoint::from(ExtendedPoint::from(P)))) .map(|p| p.map(|P| CachedPoint::from(ExtendedPoint::from(P))))
.collect::<Option<Vec<_>>>() { .collect::<Option<Vec<_>>>()
{
Some(x) => x, Some(x) => x,
None => return None, None => return None,
}; };
@ -70,18 +70,17 @@ impl VartimeMultiscalarMul for Pippenger {
.collect(); .collect();
let mut columns = (0..digits_count).rev().map(|digit_index| { let mut columns = (0..digits_count).rev().map(|digit_index| {
// Clear the buckets when processing another digit. // Clear the buckets when processing another digit.
for i in 0..buckets_count { for i in 0..buckets_count {
buckets[i] = ExtendedPoint::identity(); buckets[i] = ExtendedPoint::identity();
} }
// Iterate over pairs of (point, scalar) // Iterate over pairs of (point, scalar)
// and add/sub the point to the corresponding bucket. // and add/sub the point to the corresponding bucket.
// Note: if we add support for precomputed lookup tables, // Note: if we add support for precomputed lookup tables,
// we'll be adding/subtractiong point premultiplied by `digits[i]` to buckets[0]. // we'll be adding/subtractiong point premultiplied by `digits[i]` to buckets[0].
for (digits, pt) in scalars.iter().zip(points.iter()) { for (digits, pt) in scalars.iter().zip(points.iter()) {
let digit = digits[digit_index]; let digit = digits[digit_index];
if digit > 0 { if digit > 0 {
let b = (digit - 1) as usize; let b = (digit - 1) as usize;
buckets[b] = &buckets[b] + pt; buckets[b] = &buckets[b] + pt;
@ -102,7 +101,8 @@ impl VartimeMultiscalarMul for Pippenger {
let mut buckets_intermediate_sum = buckets[buckets_count - 1]; let mut buckets_intermediate_sum = buckets[buckets_count - 1];
let mut buckets_sum = buckets[buckets_count - 1]; let mut buckets_sum = buckets[buckets_count - 1];
for i in (0..(buckets_count - 1)).rev() { for i in (0..(buckets_count - 1)).rev() {
buckets_intermediate_sum = &buckets_intermediate_sum + &CachedPoint::from(buckets[i]); buckets_intermediate_sum =
&buckets_intermediate_sum + &CachedPoint::from(buckets[i]);
buckets_sum = &buckets_sum + &CachedPoint::from(buckets_intermediate_sum); buckets_sum = &buckets_sum + &CachedPoint::from(buckets_intermediate_sum);
} }
@ -114,9 +114,11 @@ impl VartimeMultiscalarMul for Pippenger {
let hi_column = columns.next().unwrap(); let hi_column = columns.next().unwrap();
Some( Some(
columns.fold(hi_column, |total, p| { columns
&total.mul_by_pow_2(w as u32) + &CachedPoint::from(p) .fold(hi_column, |total, p| {
}).into() &total.mul_by_pow_2(w as u32) + &CachedPoint::from(p)
})
.into(),
) )
} }
} }
@ -134,12 +136,10 @@ mod test {
let x = Scalar::from(2128506u64).invert(); let x = Scalar::from(2128506u64).invert();
let y = Scalar::from(4443282u64).invert(); let y = Scalar::from(4443282u64).invert();
let points: Vec<_> = (0..n) let points: Vec<_> = (0..n)
.map(|i| { .map(|i| constants::ED25519_BASEPOINT_POINT * Scalar::from(1 + i as u64))
constants::ED25519_BASEPOINT_POINT * Scalar::from(1 + i as u64)
})
.collect(); .collect();
let scalars: Vec<_> = (0..n) let scalars: Vec<_> = (0..n)
.map(|i| x + (Scalar::from(i as u64)*y)) // fast way to make ~random but deterministic scalars .map(|i| x + (Scalar::from(i as u64) * y)) // fast way to make ~random but deterministic scalars
.collect(); .collect();
let premultiplied: Vec<EdwardsPoint> = scalars let premultiplied: Vec<EdwardsPoint> = scalars
@ -160,4 +160,4 @@ mod test {
n = n / 2; n = n / 2;
} }
} }
} }