From e3d84280b9317eb820600f993792d7dc8115768d Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 14 May 2020 20:30:22 +1200 Subject: [PATCH] group: Sum + for<'a> Sum<&'a Self> bounds for Group --- bellman/src/groth16/tests/dummy_engine.rs | 13 +++++++++++++ group/src/lib.rs | 3 +++ pairing/src/bls12_381/ec.rs | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/bellman/src/groth16/tests/dummy_engine.rs b/bellman/src/groth16/tests/dummy_engine.rs index 611be9752..41ee7252a 100644 --- a/bellman/src/groth16/tests/dummy_engine.rs +++ b/bellman/src/groth16/tests/dummy_engine.rs @@ -4,6 +4,7 @@ use pairing::{Engine, PairingCurveAffine}; use rand_core::RngCore; use std::fmt; +use std::iter::Sum; use std::num::Wrapping; use std::ops::{Add, AddAssign, BitAnd, Mul, MulAssign, Neg, Shr, Sub, SubAssign}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -47,6 +48,18 @@ impl ConditionallySelectable for Fr { } } +impl Sum for Fr { + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ::std::ops::Add::add) + } +} + +impl<'r> Sum<&'r Fr> for Fr { + fn sum>(iter: I) -> Self { + iter.fold(Self::zero(), ::std::ops::Add::add) + } +} + impl Neg for Fr { type Output = Self; diff --git a/group/src/lib.rs b/group/src/lib.rs index fc51af2ca..ab0e82744 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -5,6 +5,7 @@ use ff::{Field, PrimeField}; use rand::RngCore; use std::error::Error; use std::fmt; +use std::iter::Sum; use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; pub mod tests; @@ -38,6 +39,8 @@ pub trait Group: + Send + Sync + 'static + + Sum + + for<'a> Sum<&'a Self> + Neg + GroupOps + GroupOpsOwned diff --git a/pairing/src/bls12_381/ec.rs b/pairing/src/bls12_381/ec.rs index 757076fcf..e5982c606 100644 --- a/pairing/src/bls12_381/ec.rs +++ b/pairing/src/bls12_381/ec.rs @@ -205,6 +205,18 @@ macro_rules! curve_impl { } } + impl ::std::iter::Sum for $projective { + fn sum>(iter: I) -> Self { + iter.fold(Self::identity(), ::std::ops::Add::add) + } + } + + impl<'r> ::std::iter::Sum<&'r $projective> for $projective { + fn sum>(iter: I) -> Self { + iter.fold(Self::identity(), ::std::ops::Add::add) + } + } + impl ::std::ops::Neg for $projective { type Output = Self;