Move from Field::negate to Neg operator

This commit is contained in:
Jack Grigg
2019-12-12 22:52:17 +00:00
parent 4a3350bc31
commit 91c32f1c7c
26 changed files with 175 additions and 189 deletions

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 {