Make Field::double take &self and return Self

This commit is contained in:
Jack Grigg
2019-12-12 22:59:18 +00:00
parent 91c32f1c7c
commit 9dac748224
23 changed files with 87 additions and 111 deletions

View File

@@ -322,11 +322,10 @@ macro_rules! curve_impl {
d.square();
d.sub_assign(&a);
d.sub_assign(&c);
d.double();
d = d.double();
// E = 3*A
let mut e = a;
e.double();
let mut e = a.double();
e.add_assign(&a);
// F = E^2
@@ -335,7 +334,7 @@ macro_rules! curve_impl {
// Z3 = 2*Y1*Z1
self.z.mul_assign(&self.y);
self.z.double();
self.z = self.z.double();
// X3 = F-2*D
self.x = f;
@@ -346,9 +345,7 @@ macro_rules! curve_impl {
self.y = d;
self.y.sub_assign(&self.x);
self.y.mul_assign(&e);
c.double();
c.double();
c.double();
c = c.double().double().double();
self.y.sub_assign(&c);
}
@@ -401,8 +398,7 @@ macro_rules! curve_impl {
h.sub_assign(&u1);
// I = (2*H)^2
let mut i = h;
i.double();
let mut i = h.double();
i.square();
// J = H*I
@@ -412,7 +408,7 @@ macro_rules! curve_impl {
// r = 2*(S2-S1)
let mut r = s2;
r.sub_assign(&s1);
r.double();
r = r.double();
// V = U1*I
let mut v = u1;
@@ -430,7 +426,7 @@ macro_rules! curve_impl {
self.y.sub_assign(&self.x);
self.y.mul_assign(&r);
s1.mul_assign(&j); // S1 = S1 * J * 2
s1.double();
s1 = s1.double();
self.y.sub_assign(&s1);
// Z3 = ((Z1+Z2)^2 - Z1Z1 - Z2Z2)*H
@@ -484,9 +480,7 @@ macro_rules! curve_impl {
hh.square();
// I = 4*HH
let mut i = hh;
i.double();
i.double();
let i = hh.double().double();
// J = H*I
let mut j = h;
@@ -495,7 +489,7 @@ macro_rules! curve_impl {
// r = 2*(S2-Y1)
let mut r = s2;
r.sub_assign(&self.y);
r.double();
r = r.double();
// V = X1*I
let mut v = self.x;
@@ -510,7 +504,7 @@ macro_rules! curve_impl {
// Y3 = r*(V-X3)-2*Y1*J
j.mul_assign(&self.y); // J = 2*Y1*J
j.double();
j = j.double();
self.y = v;
self.y.sub_assign(&self.x);
self.y.mul_assign(&r);

View File

@@ -2001,11 +2001,8 @@ fn test_fq_double() {
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fq::random(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double();
assert_eq!(a, b);
let a = Fq::random(&mut rng);
assert_eq!(a.double(), a + a);
}
}

View File

@@ -183,9 +183,11 @@ impl Field for Fq12 {
self.c0.is_zero() && self.c1.is_zero()
}
fn double(&mut self) {
self.c0.double();
self.c1.double();
fn double(&self) -> Self {
Fq12 {
c0: self.c0.double(),
c1: self.c1.double(),
}
}
fn frobenius_map(&mut self, power: usize) {

View File

@@ -213,9 +213,11 @@ impl Field for Fq2 {
self.c0 = c0;
}
fn double(&mut self) {
self.c0.double();
self.c1.double();
fn double(&self) -> Self {
Fq2 {
c0: self.c0.double(),
c1: self.c1.double(),
}
}
fn inverse(&self) -> Option<Self> {
@@ -741,7 +743,7 @@ fn test_fq2_doubling() {
use super::fq::FqRepr;
use ff::PrimeField;
let mut a = Fq2 {
let a = Fq2 {
c0: Fq::from_repr(FqRepr([
0x2d0078036923ffc7,
0x11e59ea221a3b6d2,
@@ -761,9 +763,8 @@ fn test_fq2_doubling() {
]))
.unwrap(),
};
a.double();
assert_eq!(
a,
a.double(),
Fq2 {
c0: Fq::from_repr(FqRepr([
0x5a00f006d247ff8e,

View File

@@ -286,10 +286,12 @@ impl Field for Fq6 {
self.c0.is_zero() && self.c1.is_zero() && self.c2.is_zero()
}
fn double(&mut self) {
self.c0.double();
self.c1.double();
self.c2.double();
fn double(&self) -> Self {
Fq6 {
c0: self.c0.double(),
c1: self.c1.double(),
c2: self.c2.double(),
}
}
fn frobenius_map(&mut self, power: usize) {
@@ -306,16 +308,14 @@ impl Field for Fq6 {
s0.square();
let mut ab = self.c0;
ab.mul_assign(&self.c1);
let mut s1 = ab;
s1.double();
let s1 = ab.double();
let mut s2 = self.c0;
s2.sub_assign(&self.c1);
s2.add_assign(&self.c2);
s2.square();
let mut bc = self.c1;
bc.mul_assign(&self.c2);
let mut s3 = bc;
s3.double();
let s3 = bc.double();
let mut s4 = self.c2;
s4.square();

View File

@@ -760,11 +760,8 @@ fn test_fr_double() {
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fr::random(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double();
assert_eq!(a, b);
let a = Fr::random(&mut rng);
assert_eq!(a.double(), a + a);
}
}

View File

@@ -199,10 +199,9 @@ impl G2Prepared {
tmp3.square();
tmp3.sub_assign(&tmp0);
tmp3.sub_assign(&tmp2);
tmp3.double();
tmp3 = tmp3.double();
let mut tmp4 = tmp0;
tmp4.double();
let mut tmp4 = tmp0.double();
tmp4.add_assign(&tmp0);
let mut tmp6 = r.x;
@@ -227,29 +226,25 @@ impl G2Prepared {
r.y.sub_assign(&r.x);
r.y.mul_assign(&tmp4);
tmp2.double();
tmp2.double();
tmp2.double();
tmp2 = tmp2.double().double().double();
r.y.sub_assign(&tmp2);
tmp3 = tmp4;
tmp3.mul_assign(&zsquared);
tmp3.double();
tmp3 = tmp3.neg();
tmp3 = tmp3.double().neg();
tmp6.square();
tmp6.sub_assign(&tmp0);
tmp6.sub_assign(&tmp5);
tmp1.double();
tmp1.double();
tmp1 = tmp1.double().double();
tmp6.sub_assign(&tmp1);
tmp0 = r.z;
tmp0.mul_assign(&zsquared);
tmp0.double();
tmp0 = tmp0.double();
(tmp0, tmp3, tmp6)
}
@@ -278,9 +273,7 @@ impl G2Prepared {
let mut t3 = t2;
t3.square();
let mut t4 = t3;
t4.double();
t4.double();
let t4 = t3.double().double();
let mut t5 = t4;
t5.mul_assign(&t2);
@@ -315,7 +308,7 @@ impl G2Prepared {
t0 = r.y;
t0.mul_assign(&t5);
t0.double();
t0 = t0.double();
r.y = t8;
r.y.sub_assign(&t0);
@@ -328,16 +321,14 @@ impl G2Prepared {
t10.sub_assign(&ztsquared);
t9.double();
t9 = t9.double();
t9.sub_assign(&t10);
t10 = r.z;
t10.double();
t10 = r.z.double();
t6 = t6.neg();
t1 = t6;
t1.double();
t1 = t6.double();
(t10, t1, t9)
}

View File

@@ -211,12 +211,8 @@ fn random_negation_tests<F: Field, R: RngCore>(rng: &mut R) {
fn random_doubling_tests<F: Field, R: RngCore>(rng: &mut R) {
for _ in 0..10000 {
let mut a = F::random(rng);
let mut b = a;
a.add_assign(&b);
b.double();
assert_eq!(a, b);
let a = F::random(rng);
assert_eq!(a + a, a.double());
}
}