Move from Field::negate to Neg operator

This commit is contained in:
Jack Grigg 2019-12-12 22:52:17 +00:00
parent c05b957e9d
commit c0cea09d13
5 changed files with 25 additions and 28 deletions

View File

@ -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);
}

View File

@ -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::<Bls12>::new();

View File

@ -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<E: ScalarEngine> TestConstraintSystem<E> {
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)]))

View File

@ -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<u32> = Wrapping(64513);
@ -22,6 +22,17 @@ impl fmt::Display for Fr {
}
}
impl Neg for Fr {
type Output = Self;
fn neg(mut self) -> Self {
if !<Fr as Field>::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 !<Fr as Field>::is_zero(self) {
self.0 = MODULUS_R - self.0;
}
}
fn inverse(&self) -> Option<Self> {
if <Fr as Field>::is_zero(self) {
None
@ -413,7 +418,7 @@ impl CurveProjective for Fr {
}
fn negate(&mut self) {
<Fr as Field>::negate(self);
self.0 = self.neg().0;
}
fn mul_assign<S: Into<<Self::Scalar as PrimeField>::Repr>>(&mut self, other: S) {
@ -495,7 +500,7 @@ impl CurveAffine for Fr {
}
fn negate(&mut self) {
<Fr as Field>::negate(self);
self.0 = self.neg().0;
}
fn mul<S: Into<<Self::Scalar as PrimeField>::Repr>>(&self, other: S) -> Self::Projective {

View File

@ -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<E: ScalarEngine> Sub<(E::Fr, Variable)> for LinearCombination<E> {
type Output = LinearCombination<E>;
#[allow(clippy::suspicious_arithmetic_impl)]
fn sub(self, (mut coeff, var): (E::Fr, Variable)) -> LinearCombination<E> {
coeff.negate();
self + (coeff, var)
fn sub(self, (coeff, var): (E::Fr, Variable)) -> LinearCombination<E> {
self + (coeff.neg(), var)
}
}