diff --git a/src/gadgets/lookup.rs b/src/gadgets/lookup.rs index 3be3ed9..bde86e2 100644 --- a/src/gadgets/lookup.rs +++ b/src/gadgets/lookup.rs @@ -1,7 +1,7 @@ //! Window table lookup gadgets. use ff::{Field, ScalarEngine}; -use std::ops::AddAssign; +use std::ops::{AddAssign, Neg}; use super::boolean::Boolean; use super::num::{AllocatedNum, Num}; @@ -16,8 +16,7 @@ where assert_eq!(assignment.len(), 1 << window_size); for (i, constant) in constants.into_iter().enumerate() { - let mut cur = assignment[i]; - cur.negate(); + let mut cur = assignment[i].neg(); cur.add_assign(constant); assignment[i] = cur; for (j, eval) in assignment.iter_mut().enumerate().skip(i + 1) { @@ -151,7 +150,7 @@ where let y = AllocatedNum::alloc(cs.namespace(|| "y"), || { let mut tmp = coords[*i.get()?].1; if *bits[2].get_value().get()? { - tmp.negate(); + tmp = tmp.neg(); } Ok(tmp) })?; @@ -281,7 +280,7 @@ mod test { assert_eq!(res.0.get_value().unwrap(), points[index].0); let mut tmp = points[index].1; if c_val { - tmp.negate() + tmp = tmp.neg() } assert_eq!(res.1.get_value().unwrap(), tmp); } diff --git a/src/gadgets/num.rs b/src/gadgets/num.rs index da3e4a0..bce55ce 100644 --- a/src/gadgets/num.rs +++ b/src/gadgets/num.rs @@ -417,7 +417,7 @@ mod test { use pairing::bls12_381::{Bls12, Fr}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; - use std::ops::SubAssign; + use std::ops::{Neg, SubAssign}; use super::{AllocatedNum, Boolean}; use crate::gadgets::test::*; @@ -519,8 +519,7 @@ mod test { #[test] fn test_into_bits_strict() { - let mut negone = Fr::one(); - negone.negate(); + let negone = Fr::one().neg(); let mut cs = TestConstraintSystem::::new(); diff --git a/src/gadgets/test/mod.rs b/src/gadgets/test/mod.rs index f0668b4..f4cc927 100644 --- a/src/gadgets/test/mod.rs +++ b/src/gadgets/test/mod.rs @@ -6,7 +6,7 @@ use crate::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable use std::collections::HashMap; use std::fmt::Write; -use std::ops::{AddAssign, MulAssign}; +use std::ops::{AddAssign, MulAssign, Neg}; use byteorder::{BigEndian, ByteOrder}; use std::cmp::Ordering; @@ -152,11 +152,7 @@ impl TestConstraintSystem { pub fn pretty_print(&self) -> String { let mut s = String::new(); - let negone = { - let mut tmp = E::Fr::one(); - tmp.negate(); - tmp - }; + let negone = E::Fr::one().neg(); let powers_of_two = (0..E::Fr::NUM_BITS) .map(|i| E::Fr::from_str("2").unwrap().pow(&[u64::from(i)])) diff --git a/src/groth16/tests/dummy_engine.rs b/src/groth16/tests/dummy_engine.rs index 93c2b75..1e8191b 100644 --- a/src/groth16/tests/dummy_engine.rs +++ b/src/groth16/tests/dummy_engine.rs @@ -9,7 +9,7 @@ use rand_core::RngCore; use std::cmp::Ordering; use std::fmt; use std::num::Wrapping; -use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; const MODULUS_R: Wrapping = Wrapping(64513); @@ -22,6 +22,17 @@ impl fmt::Display for Fr { } } +impl Neg for Fr { + type Output = Self; + + fn neg(mut self) -> Self { + if !::is_zero(&self) { + self.0 = MODULUS_R - self.0; + } + self + } +} + impl<'r> Add<&'r Fr> for Fr { type Output = Self; @@ -137,12 +148,6 @@ impl Field for Fr { self.0 = (self.0 << 1) % MODULUS_R; } - fn negate(&mut self) { - if !::is_zero(self) { - self.0 = MODULUS_R - self.0; - } - } - fn inverse(&self) -> Option { if ::is_zero(self) { None @@ -413,7 +418,7 @@ impl CurveProjective for Fr { } fn negate(&mut self) { - ::negate(self); + self.0 = self.neg().0; } fn mul_assign::Repr>>(&mut self, other: S) { @@ -495,7 +500,7 @@ impl CurveAffine for Fr { } fn negate(&mut self) { - ::negate(self); + self.0 = self.neg().0; } fn mul::Repr>>(&self, other: S) -> Self::Projective { diff --git a/src/lib.rs b/src/lib.rs index d29fe7d..a6eea41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,7 +161,7 @@ use std::error::Error; use std::fmt; use std::io; use std::marker::PhantomData; -use std::ops::{Add, MulAssign, Sub}; +use std::ops::{Add, MulAssign, Neg, Sub}; /// Computations are expressed in terms of arithmetic circuits, in particular /// rank-1 quadratic constraint systems. The `Circuit` trait represents a @@ -229,10 +229,8 @@ impl Sub<(E::Fr, Variable)> for LinearCombination { type Output = LinearCombination; #[allow(clippy::suspicious_arithmetic_impl)] - fn sub(self, (mut coeff, var): (E::Fr, Variable)) -> LinearCombination { - coeff.negate(); - - self + (coeff, var) + fn sub(self, (coeff, var): (E::Fr, Variable)) -> LinearCombination { + self + (coeff.neg(), var) } }