mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-07-30 20:11:23 +00:00
Move from Field::negate to Neg operator
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use rand_core::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use pairing::bls12_381::*;
|
||||
@@ -236,7 +236,7 @@ fn bench_fq_inverse(b: &mut ::test::Bencher) {
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_fq_negate(b: &mut ::test::Bencher) {
|
||||
fn bench_fq_neg(b: &mut ::test::Bencher) {
|
||||
const SAMPLES: usize = 1000;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([
|
||||
@@ -248,8 +248,7 @@ fn bench_fq_negate(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.negate();
|
||||
let tmp = v[count].neg();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use rand_core::SeedableRng;
|
||||
use rand_xorshift::XorShiftRng;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField};
|
||||
use pairing::bls12_381::*;
|
||||
@@ -236,7 +236,7 @@ fn bench_fr_inverse(b: &mut ::test::Bencher) {
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_fr_negate(b: &mut ::test::Bencher) {
|
||||
fn bench_fr_neg(b: &mut ::test::Bencher) {
|
||||
const SAMPLES: usize = 1000;
|
||||
|
||||
let mut rng = XorShiftRng::from_seed([
|
||||
@@ -248,8 +248,7 @@ fn bench_fr_negate(b: &mut ::test::Bencher) {
|
||||
|
||||
let mut count = 0;
|
||||
b.iter(|| {
|
||||
let mut tmp = v[count];
|
||||
tmp.negate();
|
||||
let tmp = v[count].neg();
|
||||
count = (count + 1) % SAMPLES;
|
||||
tmp
|
||||
});
|
||||
|
@@ -107,8 +107,7 @@ macro_rules! curve_impl {
|
||||
x3b.add_assign(&$affine::get_coeff_b());
|
||||
|
||||
x3b.sqrt().map(|y| {
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
|
||||
$affine {
|
||||
x: x,
|
||||
@@ -171,7 +170,7 @@ macro_rules! curve_impl {
|
||||
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
self.y.negate();
|
||||
self.y = self.y.neg();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,7 +526,7 @@ macro_rules! curve_impl {
|
||||
|
||||
fn negate(&mut self) {
|
||||
if !self.is_zero() {
|
||||
self.y.negate()
|
||||
self.y = self.y.neg();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,7 +626,7 @@ pub mod g1 {
|
||||
use group::{CurveAffine, CurveProjective, EncodedPoint, GroupDecodingError};
|
||||
use rand_core::RngCore;
|
||||
use std::fmt;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
curve_impl!(
|
||||
"G1",
|
||||
@@ -849,8 +848,7 @@ pub mod g1 {
|
||||
affine.x.into_repr().write_be(&mut writer).unwrap();
|
||||
}
|
||||
|
||||
let mut negy = affine.y;
|
||||
negy.negate();
|
||||
let negy = affine.y.neg();
|
||||
|
||||
// Set the third most significant bit if the correct y-coordinate
|
||||
// is lexicographically largest.
|
||||
@@ -948,8 +946,7 @@ pub mod g1 {
|
||||
|
||||
if let Some(y) = rhs.sqrt() {
|
||||
let yrepr = y.into_repr();
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
let negyrepr = negy.into_repr();
|
||||
|
||||
let p = G1Affine {
|
||||
@@ -1297,7 +1294,7 @@ pub mod g2 {
|
||||
use group::{CurveAffine, CurveProjective, EncodedPoint, GroupDecodingError};
|
||||
use rand_core::RngCore;
|
||||
use std::fmt;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
curve_impl!(
|
||||
"G2",
|
||||
@@ -1544,8 +1541,7 @@ pub mod g2 {
|
||||
affine.x.c0.into_repr().write_be(&mut writer).unwrap();
|
||||
}
|
||||
|
||||
let mut negy = affine.y;
|
||||
negy.negate();
|
||||
let negy = affine.y.neg();
|
||||
|
||||
// Set the third most significant bit if the correct y-coordinate
|
||||
// is lexicographically largest.
|
||||
@@ -1654,8 +1650,7 @@ pub mod g2 {
|
||||
rhs.add_assign(&G2Affine::get_coeff_b());
|
||||
|
||||
if let Some(y) = rhs.sqrt() {
|
||||
let mut negy = y;
|
||||
negy.negate();
|
||||
let negy = y.neg();
|
||||
|
||||
let p = G2Affine {
|
||||
x,
|
||||
|
@@ -2,6 +2,9 @@ use super::fq2::Fq2;
|
||||
use ff::{Field, PrimeField, PrimeFieldDecodingError, PrimeFieldRepr};
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
|
||||
#[cfg(test)]
|
||||
use std::ops::Neg;
|
||||
|
||||
// B coefficient of BLS12-381 curve, 4.
|
||||
pub const B_COEFF: Fq = Fq(FqRepr([
|
||||
0xaa270000000cfff3,
|
||||
@@ -456,8 +459,7 @@ fn test_b_coeff() {
|
||||
|
||||
#[test]
|
||||
fn test_frob_coeffs() {
|
||||
let mut nqr = Fq::one();
|
||||
nqr.negate();
|
||||
let nqr = Fq::one().neg();
|
||||
|
||||
assert_eq!(FROBENIUS_COEFF_FQ2_C1[0], Fq::one());
|
||||
assert_eq!(
|
||||
@@ -1167,8 +1169,7 @@ fn test_frob_coeffs() {
|
||||
|
||||
#[test]
|
||||
fn test_neg_one() {
|
||||
let mut o = Fq::one();
|
||||
o.negate();
|
||||
let o = Fq::one().neg();
|
||||
|
||||
assert_eq!(NEGATIVE_ONE, o);
|
||||
}
|
||||
@@ -2009,10 +2010,9 @@ fn test_fq_double() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fq_negate() {
|
||||
fn test_fq_neg() {
|
||||
{
|
||||
let mut a = Fq::zero();
|
||||
a.negate();
|
||||
let a = Fq::zero().neg();
|
||||
|
||||
assert!(a.is_zero());
|
||||
}
|
||||
@@ -2025,8 +2025,7 @@ fn test_fq_negate() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure (a - (-a)) = 0.
|
||||
let mut a = Fq::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let b = a.neg();
|
||||
a.add_assign(&b);
|
||||
|
||||
assert!(a.is_zero());
|
||||
@@ -2074,8 +2073,7 @@ fn test_fq_sqrt() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure sqrt(a^2) = a or -a
|
||||
let a = Fq::random(&mut rng);
|
||||
let mut nega = a;
|
||||
nega.negate();
|
||||
let nega = a.neg();
|
||||
let mut b = a;
|
||||
b.square();
|
||||
|
||||
|
@@ -3,7 +3,7 @@ use super::fq2::Fq2;
|
||||
use super::fq6::Fq6;
|
||||
use ff::Field;
|
||||
use rand_core::RngCore;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
/// An element of Fq12, represented by c0 + c1 * w.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@@ -20,7 +20,7 @@ impl ::std::fmt::Display for Fq12 {
|
||||
|
||||
impl Fq12 {
|
||||
pub fn conjugate(&mut self) {
|
||||
self.c1.negate();
|
||||
self.c1 = self.c1.neg();
|
||||
}
|
||||
|
||||
pub fn mul_by_014(&mut self, c0: &Fq2, c1: &Fq2, c4: &Fq2) {
|
||||
@@ -40,6 +40,17 @@ impl Fq12 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq12 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq12 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq12> for Fq12 {
|
||||
type Output = Self;
|
||||
|
||||
@@ -177,11 +188,6 @@ impl Field for Fq12 {
|
||||
self.c1.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
}
|
||||
|
||||
fn frobenius_map(&mut self, power: usize) {
|
||||
self.c0.frobenius_map(power);
|
||||
self.c1.frobenius_map(power);
|
||||
@@ -216,13 +222,9 @@ impl Field for Fq12 {
|
||||
c1s.mul_by_nonresidue();
|
||||
c0s.sub_assign(&c1s);
|
||||
|
||||
c0s.inverse().map(|t| {
|
||||
let mut tmp = Fq12 { c0: t, c1: t };
|
||||
tmp.c0.mul_assign(&self.c0);
|
||||
tmp.c1.mul_assign(&self.c1);
|
||||
tmp.c1.negate();
|
||||
|
||||
tmp
|
||||
c0s.inverse().map(|t| Fq12 {
|
||||
c0: t.mul(&self.c0),
|
||||
c1: t.mul(&self.c1).neg(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@ use super::fq::{Fq, FROBENIUS_COEFF_FQ2_C1, NEGATIVE_ONE};
|
||||
use ff::{Field, SqrtField};
|
||||
use rand_core::RngCore;
|
||||
use std::cmp::Ordering;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
/// An element of Fq2, represented by c0 + c1 * u.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@@ -56,6 +56,17 @@ impl Fq2 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq2 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq2 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq2> for Fq2 {
|
||||
type Output = Self;
|
||||
|
||||
@@ -192,8 +203,7 @@ impl Field for Fq2 {
|
||||
ab.mul_assign(&self.c1);
|
||||
let mut c0c1 = self.c0;
|
||||
c0c1.add_assign(&self.c1);
|
||||
let mut c0 = self.c1;
|
||||
c0.negate();
|
||||
let mut c0 = self.c1.neg();
|
||||
c0.add_assign(&self.c0);
|
||||
c0.mul_assign(&c0c1);
|
||||
c0.sub_assign(&ab);
|
||||
@@ -208,27 +218,15 @@ impl Field for Fq2 {
|
||||
self.c1.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Self> {
|
||||
let mut t1 = self.c1;
|
||||
t1.square();
|
||||
let mut t0 = self.c0;
|
||||
t0.square();
|
||||
t0.add_assign(&t1);
|
||||
t0.inverse().map(|t| {
|
||||
let mut tmp = Fq2 {
|
||||
c0: self.c0,
|
||||
c1: self.c1,
|
||||
};
|
||||
tmp.c0.mul_assign(&t);
|
||||
tmp.c1.mul_assign(&t);
|
||||
tmp.c1.negate();
|
||||
|
||||
tmp
|
||||
t0.inverse().map(|t| Fq2 {
|
||||
c0: self.c0.mul(&t),
|
||||
c1: self.c1.mul(&t).neg(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -372,10 +370,8 @@ fn test_fq2_squaring() {
|
||||
}; // u
|
||||
a.square();
|
||||
assert_eq!(a, {
|
||||
let mut neg1 = Fq::one();
|
||||
neg1.negate();
|
||||
Fq2 {
|
||||
c0: neg1,
|
||||
c0: Fq::one().neg(),
|
||||
c1: Fq::zero(),
|
||||
}
|
||||
}); // -1
|
||||
@@ -694,7 +690,7 @@ fn test_fq2_negation() {
|
||||
use super::fq::FqRepr;
|
||||
use ff::PrimeField;
|
||||
|
||||
let mut a = Fq2 {
|
||||
let a = Fq2 {
|
||||
c0: Fq::from_repr(FqRepr([
|
||||
0x2d0078036923ffc7,
|
||||
0x11e59ea221a3b6d2,
|
||||
@@ -713,8 +709,8 @@ fn test_fq2_negation() {
|
||||
0x12d1137b8a6a837,
|
||||
]))
|
||||
.unwrap(),
|
||||
};
|
||||
a.negate();
|
||||
}
|
||||
.neg();
|
||||
assert_eq!(
|
||||
a,
|
||||
Fq2 {
|
||||
@@ -1000,8 +996,7 @@ fn test_fq2_legendre() {
|
||||
|
||||
assert_eq!(Zero, Fq2::zero().legendre());
|
||||
// i^2 = -1
|
||||
let mut m1 = Fq2::one();
|
||||
m1.negate();
|
||||
let mut m1 = Fq2::one().neg();
|
||||
assert_eq!(QuadraticResidue, m1.legendre());
|
||||
m1.mul_by_nonresidue();
|
||||
assert_eq!(QuadraticNonResidue, m1.legendre());
|
||||
|
@@ -2,7 +2,7 @@ use super::fq::{FROBENIUS_COEFF_FQ6_C1, FROBENIUS_COEFF_FQ6_C2};
|
||||
use super::fq2::Fq2;
|
||||
use ff::Field;
|
||||
use rand_core::RngCore;
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
|
||||
/// An element of Fq6, represented by c0 + c1 * v + c2 * v^(2).
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
@@ -100,6 +100,18 @@ impl Fq6 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Fq6 {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self {
|
||||
Fq6 {
|
||||
c0: self.c0.neg(),
|
||||
c1: self.c1.neg(),
|
||||
c2: self.c2.neg(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Add<&'r Fq6> for Fq6 {
|
||||
type Output = Self;
|
||||
|
||||
@@ -280,12 +292,6 @@ impl Field for Fq6 {
|
||||
self.c2.double();
|
||||
}
|
||||
|
||||
fn negate(&mut self) {
|
||||
self.c0.negate();
|
||||
self.c1.negate();
|
||||
self.c2.negate();
|
||||
}
|
||||
|
||||
fn frobenius_map(&mut self, power: usize) {
|
||||
self.c0.frobenius_map(power);
|
||||
self.c1.frobenius_map(power);
|
||||
@@ -332,7 +338,7 @@ impl Field for Fq6 {
|
||||
let mut c0 = self.c2;
|
||||
c0.mul_by_nonresidue();
|
||||
c0.mul_assign(&self.c1);
|
||||
c0.negate();
|
||||
c0 = c0.neg();
|
||||
{
|
||||
let mut c0s = self.c0;
|
||||
c0s.square();
|
||||
|
@@ -10,6 +10,8 @@ pub struct Fr(FrRepr);
|
||||
use rand_core::SeedableRng;
|
||||
#[cfg(test)]
|
||||
use rand_xorshift::XorShiftRng;
|
||||
#[cfg(test)]
|
||||
use std::ops::Neg;
|
||||
|
||||
#[test]
|
||||
fn test_fr_repr_ordering() {
|
||||
@@ -767,10 +769,9 @@ fn test_fr_double() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fr_negate() {
|
||||
fn test_fr_neg() {
|
||||
{
|
||||
let mut a = Fr::zero();
|
||||
a.negate();
|
||||
let a = Fr::zero().neg();
|
||||
|
||||
assert!(a.is_zero());
|
||||
}
|
||||
@@ -783,8 +784,7 @@ fn test_fr_negate() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure (a - (-a)) = 0.
|
||||
let mut a = Fr::random(&mut rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let b = a.neg();
|
||||
a.add_assign(&b);
|
||||
|
||||
assert!(a.is_zero());
|
||||
@@ -832,8 +832,7 @@ fn test_fr_sqrt() {
|
||||
for _ in 0..1000 {
|
||||
// Ensure sqrt(a^2) = a or -a
|
||||
let a = Fr::random(&mut rng);
|
||||
let mut nega = a;
|
||||
nega.negate();
|
||||
let nega = a.neg();
|
||||
let mut b = a;
|
||||
b.square();
|
||||
|
||||
|
@@ -25,7 +25,7 @@ use super::{Engine, PairingCurveAffine};
|
||||
|
||||
use ff::{BitIterator, Field, ScalarEngine};
|
||||
use group::CurveAffine;
|
||||
use std::ops::{AddAssign, MulAssign, SubAssign};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
// The BLS parameter x for BLS12-381 is -0xd201000000010000
|
||||
const BLS_X: u64 = 0xd201000000010000;
|
||||
@@ -236,7 +236,7 @@ impl G2Prepared {
|
||||
tmp3 = tmp4;
|
||||
tmp3.mul_assign(&zsquared);
|
||||
tmp3.double();
|
||||
tmp3.negate();
|
||||
tmp3 = tmp3.neg();
|
||||
|
||||
tmp6.square();
|
||||
tmp6.sub_assign(&tmp0);
|
||||
@@ -334,7 +334,7 @@ impl G2Prepared {
|
||||
t10 = r.z;
|
||||
t10.double();
|
||||
|
||||
t6.negate();
|
||||
t6 = t6.neg();
|
||||
|
||||
t1 = t6;
|
||||
t1.double();
|
||||
|
@@ -36,8 +36,7 @@ pub fn random_sqrt_tests<F: SqrtField>() {
|
||||
assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
|
||||
|
||||
let b = b.sqrt().unwrap();
|
||||
let mut negb = b;
|
||||
negb.negate();
|
||||
let negb = b.neg();
|
||||
|
||||
assert!(a == b || a == negb);
|
||||
}
|
||||
@@ -51,7 +50,7 @@ pub fn random_sqrt_tests<F: SqrtField>() {
|
||||
b = b.sqrt().unwrap();
|
||||
|
||||
if b != c {
|
||||
b.negate();
|
||||
b = b.neg();
|
||||
}
|
||||
|
||||
assert_eq!(b, c);
|
||||
@@ -77,8 +76,7 @@ pub fn random_field_tests<F: Field>() {
|
||||
|
||||
assert!(F::zero().is_zero());
|
||||
{
|
||||
let mut z = F::zero();
|
||||
z.negate();
|
||||
let z = F::zero().neg();
|
||||
assert!(z.is_zero());
|
||||
}
|
||||
|
||||
@@ -204,8 +202,7 @@ fn random_subtraction_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
fn random_negation_tests<F: Field, R: RngCore>(rng: &mut R) {
|
||||
for _ in 0..10000 {
|
||||
let a = F::random(rng);
|
||||
let mut b = a;
|
||||
b.negate();
|
||||
let mut b = a.neg();
|
||||
b.add_assign(&a);
|
||||
|
||||
assert!(b.is_zero());
|
||||
|
Reference in New Issue
Block a user