diff --git a/bellman/src/groth16/tests/dummy_engine.rs b/bellman/src/groth16/tests/dummy_engine.rs index f75be5369..504030fd7 100644 --- a/bellman/src/groth16/tests/dummy_engine.rs +++ b/bellman/src/groth16/tests/dummy_engine.rs @@ -417,10 +417,6 @@ impl CurveProjective for Fr { AddAssign::add_assign(self, other); } - fn negate(&mut self) { - self.0 = self.neg().0; - } - fn mul_assign::Repr>>(&mut self, other: S) { let tmp = Fr::from_repr(other.into()).unwrap(); @@ -499,10 +495,6 @@ impl CurveAffine for Fr { ::is_zero(self) } - fn negate(&mut self) { - self.0 = self.neg().0; - } - fn mul::Repr>>(&self, other: S) -> Self::Projective { let mut res = *self; let tmp = Fr::from_repr(other.into()).unwrap(); diff --git a/bellman/src/groth16/verifier.rs b/bellman/src/groth16/verifier.rs index 3d9ff0881..8ee8a7418 100644 --- a/bellman/src/groth16/verifier.rs +++ b/bellman/src/groth16/verifier.rs @@ -1,17 +1,15 @@ use ff::PrimeField; use group::{CurveAffine, CurveProjective}; use pairing::{Engine, PairingCurveAffine}; -use std::ops::AddAssign; +use std::ops::{AddAssign, Neg}; use super::{PreparedVerifyingKey, Proof, VerifyingKey}; use crate::SynthesisError; pub fn prepare_verifying_key(vk: &VerifyingKey) -> PreparedVerifyingKey { - let mut gamma = vk.gamma_g2; - gamma.negate(); - let mut delta = vk.delta_g2; - delta.negate(); + let gamma = vk.gamma_g2.neg(); + let delta = vk.delta_g2.neg(); PreparedVerifyingKey { alpha_g1_beta_g2: E::pairing(vk.alpha_g1, vk.beta_g2), diff --git a/group/src/lib.rs b/group/src/lib.rs index 8d5c405a6..b2dc410ea 100644 --- a/group/src/lib.rs +++ b/group/src/lib.rs @@ -5,7 +5,7 @@ use ff::{PrimeField, PrimeFieldDecodingError, ScalarEngine, SqrtField}; use rand::RngCore; use std::error::Error; use std::fmt; -use std::ops::{Add, AddAssign, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; pub mod tests; @@ -27,6 +27,7 @@ pub trait CurveProjective: + 'static + Add + Sub + + Neg + for<'a> Add<&'a Self, Output = Self> + for<'a> Sub<&'a Self, Output = Self> + AddAssign @@ -65,9 +66,6 @@ pub trait CurveProjective: /// Adds an affine element to this element. fn add_assign_mixed(&mut self, other: &Self::Affine); - /// Negates this element. - fn negate(&mut self); - /// Performs scalar multiplication of this element. fn mul_assign::Repr>>(&mut self, other: S); @@ -86,7 +84,17 @@ pub trait CurveProjective: /// Affine representation of an elliptic curve point guaranteed to be /// in the correct prime order subgroup. pub trait CurveAffine: - Copy + Clone + Sized + Send + Sync + fmt::Debug + fmt::Display + PartialEq + Eq + 'static + Copy + + Clone + + Sized + + Send + + Sync + + fmt::Debug + + fmt::Display + + PartialEq + + Eq + + 'static + + Neg { type Engine: ScalarEngine; type Scalar: PrimeField + SqrtField; @@ -105,9 +113,6 @@ pub trait CurveAffine: /// additive identity. fn is_zero(&self) -> bool; - /// Negates this element. - fn negate(&mut self); - /// Performs scalar multiplication of this element with mixed addition. fn mul::Repr>>(&self, other: S) -> Self::Projective; diff --git a/group/src/tests/mod.rs b/group/src/tests/mod.rs index 949d934f2..e86167b41 100644 --- a/group/src/tests/mod.rs +++ b/group/src/tests/mod.rs @@ -13,8 +13,7 @@ pub fn curve_tests() { // Negation edge case with zero. { - let mut z = G::zero(); - z.negate(); + let z = G::zero().neg(); assert!(z.is_zero()); } @@ -216,8 +215,7 @@ fn random_negation_tests() { t4.add_assign_mixed(&t2.into_affine()); assert!(t4.is_zero()); - t1.negate(); - assert_eq!(t1, t2); + assert_eq!(t1.neg(), t2); } } @@ -440,7 +438,7 @@ fn random_encoding_tests() { let de_compressed = compressed.into_affine().unwrap(); assert_eq!(de_compressed, r); - r.negate(); + r = r.neg(); let compressed = r.into_compressed(); let de_compressed = compressed.into_affine().unwrap(); diff --git a/pairing/src/bls12_381/ec.rs b/pairing/src/bls12_381/ec.rs index 8e105f290..b016959d7 100644 --- a/pairing/src/bls12_381/ec.rs +++ b/pairing/src/bls12_381/ec.rs @@ -134,6 +134,19 @@ macro_rules! curve_impl { } } + impl ::std::ops::Neg for $affine { + type Output = Self; + + #[inline] + fn neg(self) -> Self { + let mut ret = self; + if !ret.is_zero() { + ret.y = ret.y.neg(); + } + ret + } + } + impl CurveAffine for $affine { type Engine = Bls12; type Scalar = $scalarfield; @@ -163,12 +176,6 @@ macro_rules! curve_impl { self.mul_bits(bits) } - fn negate(&mut self) { - if !self.is_zero() { - self.y = self.y.neg(); - } - } - fn into_projective(&self) -> $projective { (*self).into() } @@ -188,6 +195,19 @@ macro_rules! curve_impl { } } + impl ::std::ops::Neg for $projective { + type Output = Self; + + #[inline] + fn neg(self) -> Self { + let mut ret = self; + if !ret.is_zero() { + ret.y = ret.y.neg(); + } + ret + } + } + impl<'r> ::std::ops::Add<&'r $projective> for $projective { type Output = Self; @@ -324,9 +344,7 @@ macro_rules! curve_impl { impl<'r> ::std::ops::SubAssign<&'r $projective> for $projective { fn sub_assign(&mut self, other: &Self) { - let mut tmp = *other; - tmp.negate(); - self.add_assign(&tmp); + self.add_assign(&other.neg()); } } @@ -566,12 +584,6 @@ macro_rules! curve_impl { } } - fn negate(&mut self) { - if !self.is_zero() { - self.y = self.y.neg(); - } - } - fn mul_assign::Repr>>(&mut self, other: S) { let mut res = Self::zero();