Skip to content

Commit 207c6dd

Browse files
committed
field: add Sum trait
Also tighten the ops::Neg bound to specify that the output needs to be Self. Otherwise this bound is not really that useful.
1 parent 53664cf commit 207c6dd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/primitives/field.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Generic Field Traits
44
5-
use core::{fmt, hash, ops};
5+
use core::{fmt, hash, iter, ops};
66

77
/// A generic field.
88
pub trait Field:
@@ -13,6 +13,8 @@ pub trait Field:
1313
+ hash::Hash
1414
+ fmt::Debug
1515
+ fmt::Display
16+
+ iter::Sum
17+
+ for<'a> iter::Sum<&'a Self>
1618
+ ops::Add<Self, Output = Self>
1719
+ ops::Sub<Self, Output = Self>
1820
+ ops::AddAssign
@@ -29,7 +31,7 @@ pub trait Field:
2931
+ for<'a> ops::MulAssign<&'a Self>
3032
+ for<'a> ops::Div<&'a Self, Output = Self>
3133
+ for<'a> ops::DivAssign<&'a Self>
32-
+ ops::Neg
34+
+ ops::Neg<Output = Self>
3335
{
3436
/// The zero constant of the field.
3537
const ZERO: Self;
@@ -362,6 +364,19 @@ macro_rules! impl_ops_for_fe {
362364
self._neg()
363365
}
364366
}
367+
368+
// sum
369+
impl core::iter::Sum for $op {
370+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
371+
iter.fold(crate::primitives::Field::ZERO, |i, acc| i + acc)
372+
}
373+
}
374+
375+
impl<'s> core::iter::Sum<&'s Self> for $op {
376+
fn sum<I: Iterator<Item = &'s Self>>(iter: I) -> Self {
377+
iter.fold(crate::primitives::Field::ZERO, |i, acc| i + acc)
378+
}
379+
}
365380
};
366381
}
367382
pub(super) use impl_ops_for_fe;

src/primitives/polynomial.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ impl<F: Field> Polynomial<F> {
9090
let mut cand = F::ONE;
9191
let mut eval = self.clone();
9292
for _ in 0..F::MULTIPLICATIVE_ORDER {
93-
let sum = eval.inner.iter().cloned().fold(F::ZERO, F::add);
94-
if sum == F::ZERO {
93+
if eval.inner.iter().sum::<F>() == F::ZERO {
9594
ret.push(cand.clone());
9695
}
9796

0 commit comments

Comments
 (0)