Skip to content

Commit 6b53813

Browse files
committed
Attempt to use Intoiterator to abstract over collection types, does not work.
1 parent a3bbece commit 6b53813

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/bls12_381/ec.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,28 +334,31 @@ macro_rules! curve_impl {
334334
self.is_zero() || self.z == $basefield::one()
335335
}
336336

337-
fn batch_normalization(v: &mut [Self])
337+
fn batch_normalization<II: ?Sized>(v: &mut II)
338+
where for<'a> &'a mut II: IntoIterator<Item = &'a mut Self>,
339+
for<'a> <&'a mut II as IntoIterator>::IntoIter: DoubleEndedIterator+ExactSizeIterator
338340
{
339341
// Montgomery’s Trick and Fast Implementation of Masked AES
340342
// Genelle, Prouff and Quisquater
341343
// Section 3.2
342344

343345
// First pass: compute [a, ab, abc, ...]
344-
let mut prod = Vec::with_capacity(v.len());
346+
let mut prod = Vec::with_capacity(v.into_iter().len());
345347
let mut tmp = $basefield::one();
346-
for g in v.iter_mut()
348+
for g in v.into_iter()
347349
// Ignore normalized elements
348350
.filter(|g| !g.is_normalized())
349351
{
350352
tmp.mul_assign(&g.z);
351353
prod.push(tmp);
352354
}
355+
if prod.is_empty() { return; }
353356

354357
// Invert `tmp`.
355358
tmp = tmp.inverse().unwrap(); // Guaranteed to be nonzero.
356359

357360
// Second pass: iterate backwards to compute inverses
358-
for (g, s) in v.iter_mut()
361+
for (g, s) in v.into_iter()
359362
// Backwards
360363
.rev()
361364
// Ignore normalized elements
@@ -372,7 +375,7 @@ macro_rules! curve_impl {
372375
}
373376

374377
// Perform affine transformations
375-
for g in v.iter_mut()
378+
for g in v.into_iter()
376379
.filter(|g| !g.is_normalized())
377380
{
378381
let mut z = g.z; // 1/z

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ pub trait CurveProjective:
141141

142142
/// Normalizes a slice of projective elements so that
143143
/// conversion to affine is cheap.
144-
fn batch_normalization(v: &mut [Self]);
144+
fn batch_normalization<II: ?Sized>(v: &mut II)
145+
where for<'a> &'a mut II: IntoIterator<Item = &'a mut Self>,
146+
for<'a> <&'a mut II as IntoIterator>::IntoIter: DoubleEndedIterator+ExactSizeIterator;
147+
// TODO: Simplify using https://github.com/rust-lang/rfcs/pull/2289
145148

146149
/// Checks if the point is already "normalized" so that
147150
/// cheap affine conversion is possible.

src/tests/curve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn random_transformation_tests<G: CurveProjective>() {
375375
let expected_v = v.iter()
376376
.map(|v| v.into_affine().into_projective())
377377
.collect::<Vec<_>>();
378-
G::batch_normalization(&mut v);
378+
G::batch_normalization(v.as_mut_slice());
379379

380380
for i in &v {
381381
assert!(i.is_normalized());

0 commit comments

Comments
 (0)