Make Field::square take &self and return Self

This commit is contained in:
Jack Grigg 2019-12-12 23:09:28 +00:00
parent 26db0d6b38
commit 575ea832f4
13 changed files with 110 additions and 200 deletions

View File

@ -210,8 +210,7 @@ fn bench_fq_square(b: &mut ::test::Bencher) {
let mut count = 0;
b.iter(|| {
let mut tmp = v[count];
tmp.square();
let tmp = v[count].square();
count = (count + 1) % SAMPLES;
tmp
});
@ -264,11 +263,7 @@ fn bench_fq_sqrt(b: &mut ::test::Bencher) {
]);
let v: Vec<Fq> = (0..SAMPLES)
.map(|_| {
let mut tmp = Fq::random(&mut rng);
tmp.square();
tmp
})
.map(|_| Fq::random(&mut rng).square())
.collect();
let mut count = 0;

View File

@ -84,8 +84,7 @@ fn bench_fq12_squaring(b: &mut ::test::Bencher) {
let mut count = 0;
b.iter(|| {
let mut tmp = v[count];
tmp.square();
let tmp = v[count].square();
count = (count + 1) % SAMPLES;
tmp
});

View File

@ -84,8 +84,7 @@ fn bench_fq2_squaring(b: &mut ::test::Bencher) {
let mut count = 0;
b.iter(|| {
let mut tmp = v[count];
tmp.square();
let tmp = v[count].square();
count = (count + 1) % SAMPLES;
tmp
});

View File

@ -210,8 +210,7 @@ fn bench_fr_square(b: &mut ::test::Bencher) {
let mut count = 0;
b.iter(|| {
let mut tmp = v[count];
tmp.square();
let tmp = v[count].square();
count = (count + 1) % SAMPLES;
tmp
});
@ -264,11 +263,7 @@ fn bench_fr_sqrt(b: &mut ::test::Bencher) {
]);
let v: Vec<Fr> = (0..SAMPLES)
.map(|_| {
let mut tmp = Fr::random(&mut rng);
tmp.square();
tmp
})
.map(|_| Fr::random(&mut rng).square())
.collect();
let mut count = 0;

View File

@ -54,10 +54,8 @@ macro_rules! curve_impl {
// are equal when (X * Z^2) = (X' * Z'^2)
// and (Y * Z^3) = (Y' * Z'^3).
let mut z1 = self.z;
z1.square();
let mut z2 = other.z;
z2.square();
let mut z1 = self.z.square();
let mut z2 = other.z.square();
let mut tmp1 = self.x;
tmp1.mul_assign(&z2);
@ -101,8 +99,7 @@ macro_rules! curve_impl {
/// largest y-coordinate be selected.
fn get_point_from_x(x: $basefield, greatest: bool) -> Option<$affine> {
// Compute x^3 + b
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&$affine::get_coeff_b());
@ -122,11 +119,9 @@ macro_rules! curve_impl {
true
} else {
// Check that the point is on the curve
let mut y2 = self.y;
y2.square();
let y2 = self.y.square();
let mut x3b = self.x;
x3b.square();
let mut x3b = self.x.square();
x3b.mul_assign(&self.x);
x3b.add_assign(&Self::get_coeff_b());
@ -283,8 +278,7 @@ macro_rules! curve_impl {
// Perform affine transformations
for g in v.iter_mut().filter(|g| !g.is_normalized()) {
let mut z = g.z; // 1/z
z.square(); // 1/z^2
let mut z = g.z.square(); // 1/z^2
g.x.mul_assign(&z); // x/z^2
z.mul_assign(&g.z); // 1/z^3
g.y.mul_assign(&z); // y/z^3
@ -305,21 +299,18 @@ macro_rules! curve_impl {
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
// A = X1^2
let mut a = self.x;
a.square();
let a = self.x.square();
// B = Y1^2
let mut b = self.y;
b.square();
let b = self.y.square();
// C = B^2
let mut c = b;
c.square();
let mut c = b.square();
// D = 2*((X1+B)2-A-C)
let mut d = self.x;
d.add_assign(&b);
d.square();
d = d.square();
d.sub_assign(&a);
d.sub_assign(&c);
d = d.double();
@ -329,8 +320,7 @@ macro_rules! curve_impl {
e.add_assign(&a);
// F = E^2
let mut f = e;
f.square();
let f = e.square();
// Z3 = 2*Y1*Z1
self.z.mul_assign(&self.y);
@ -362,12 +352,10 @@ macro_rules! curve_impl {
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
// Z1Z1 = Z1^2
let mut z1z1 = self.z;
z1z1.square();
let z1z1 = self.z.square();
// Z2Z2 = Z2^2
let mut z2z2 = other.z;
z2z2.square();
let z2z2 = other.z.square();
// U1 = X1*Z2Z2
let mut u1 = self.x;
@ -398,8 +386,7 @@ macro_rules! curve_impl {
h.sub_assign(&u1);
// I = (2*H)^2
let mut i = h.double();
i.square();
let i = h.double().square();
// J = H*I
let mut j = h;
@ -415,8 +402,7 @@ macro_rules! curve_impl {
v.mul_assign(&i);
// X3 = r^2 - J - 2*V
self.x = r;
self.x.square();
self.x = r.square();
self.x.sub_assign(&j);
self.x.sub_assign(&v);
self.x.sub_assign(&v);
@ -431,7 +417,7 @@ macro_rules! curve_impl {
// Z3 = ((Z1+Z2)^2 - Z1Z1 - Z2Z2)*H
self.z.add_assign(&other.z);
self.z.square();
self.z = self.z.square();
self.z.sub_assign(&z1z1);
self.z.sub_assign(&z2z2);
self.z.mul_assign(&h);
@ -453,8 +439,7 @@ macro_rules! curve_impl {
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
// Z1Z1 = Z1^2
let mut z1z1 = self.z;
z1z1.square();
let z1z1 = self.z.square();
// U2 = X2*Z1Z1
let mut u2 = other.x;
@ -476,8 +461,7 @@ macro_rules! curve_impl {
h.sub_assign(&self.x);
// HH = H^2
let mut hh = h;
hh.square();
let hh = h.square();
// I = 4*HH
let i = hh.double().double();
@ -496,8 +480,7 @@ macro_rules! curve_impl {
v.mul_assign(&i);
// X3 = r^2 - J - 2*V
self.x = r;
self.x.square();
self.x = r.square();
self.x.sub_assign(&j);
self.x.sub_assign(&v);
self.x.sub_assign(&v);
@ -512,7 +495,7 @@ macro_rules! curve_impl {
// Z3 = (Z1+H)^2-Z1Z1-HH
self.z.add_assign(&h);
self.z.square();
self.z = self.z.square();
self.z.sub_assign(&z1z1);
self.z.sub_assign(&hh);
}
@ -589,8 +572,7 @@ macro_rules! curve_impl {
} else {
// Z is nonzero, so it must have an inverse in a field.
let zinv = p.z.inverse().unwrap();
let mut zinv_powered = zinv;
zinv_powered.square();
let mut zinv_powered = zinv.square();
// X/Z^2
let mut x = p.x;
@ -933,8 +915,7 @@ pub mod g1 {
let mut i = 0;
loop {
// y^2 = x^3 + b
let mut rhs = x;
rhs.square();
let mut rhs = x.square();
rhs.mul_assign(&x);
rhs.add_assign(&G1Affine::get_coeff_b());
@ -1638,8 +1619,7 @@ pub mod g2 {
let mut i = 0;
loop {
// y^2 = x^3 + b
let mut rhs = x;
rhs.square();
let mut rhs = x.square();
rhs.mul_assign(&x);
rhs.add_assign(&G2Affine::get_coeff_b());

View File

@ -1930,7 +1930,7 @@ fn test_fq_mul_assign() {
#[test]
fn test_fq_squaring() {
let mut a = Fq(FqRepr([
let a = Fq(FqRepr([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
@ -1939,9 +1939,8 @@ fn test_fq_squaring() {
0x19ffffffffffffff,
]));
assert!(a.is_valid());
a.square();
assert_eq!(
a,
a.square(),
Fq::from_repr(FqRepr([
0x1cfb28fe7dfbbb86,
0x24cbe1731577a59,
@ -1961,14 +1960,7 @@ fn test_fq_squaring() {
for _ in 0..1000000 {
// Ensure that (a * a) = a^2
let a = Fq::random(&mut rng);
let mut tmp = a;
tmp.square();
let mut tmp2 = a;
tmp2.mul_assign(&a);
assert_eq!(tmp, tmp2);
assert_eq!(a.square(), a * a);
}
}
@ -2071,8 +2063,7 @@ fn test_fq_sqrt() {
// Ensure sqrt(a^2) = a or -a
let a = Fq::random(&mut rng);
let nega = a.neg();
let mut b = a;
b.square();
let b = a.square();
let b = b.sqrt().unwrap();
@ -2083,10 +2074,8 @@ fn test_fq_sqrt() {
// Ensure sqrt(a)^2 = a for random a
let a = Fq::random(&mut rng);
if let Some(mut tmp) = a.sqrt() {
tmp.square();
assert_eq!(a, tmp);
if let Some(tmp) = a.sqrt() {
assert_eq!(a, tmp.square());
}
}
}

View File

@ -199,7 +199,7 @@ impl Field for Fq12 {
self.c1.c2.mul_assign(&FROBENIUS_COEFF_FQ12_C1[power % 12]);
}
fn square(&mut self) {
fn square(&self) -> Self {
let mut ab = self.c0;
ab.mul_assign(&self.c1);
let mut c0c1 = self.c0;
@ -209,18 +209,16 @@ impl Field for Fq12 {
c0.add_assign(&self.c0);
c0.mul_assign(&c0c1);
c0.sub_assign(&ab);
self.c1 = ab;
self.c1.add_assign(&ab);
let mut c1 = ab;
c1.add_assign(&ab);
ab.mul_by_nonresidue();
c0.sub_assign(&ab);
self.c0 = c0;
Fq12 { c0, c1 }
}
fn inverse(&self) -> Option<Self> {
let mut c0s = self.c0;
c0s.square();
let mut c1s = self.c1;
c1s.square();
let mut c0s = self.c0.square();
let mut c1s = self.c1.square();
c1s.mul_by_nonresidue();
c0s.sub_assign(&c1s);

View File

@ -46,10 +46,8 @@ impl Fq2 {
/// Norm of Fq2 as extension field in i over Fq
pub fn norm(&self) -> Fq {
let mut t0 = self.c0;
let mut t1 = self.c1;
t0.square();
t1.square();
let t0 = self.c0.square();
let mut t1 = self.c1.square();
t1.add_assign(&t0);
t1
@ -198,7 +196,7 @@ impl Field for Fq2 {
self.c0.is_zero() && self.c1.is_zero()
}
fn square(&mut self) {
fn square(&self) -> Self {
let mut ab = self.c0;
ab.mul_assign(&self.c1);
let mut c0c1 = self.c0;
@ -207,10 +205,10 @@ impl Field for Fq2 {
c0.add_assign(&self.c0);
c0.mul_assign(&c0c1);
c0.sub_assign(&ab);
self.c1 = ab;
self.c1.add_assign(&ab);
let mut c1 = ab;
c1.add_assign(&ab);
c0.add_assign(&ab);
self.c0 = c0;
Fq2 { c0, c1 }
}
fn double(&self) -> Self {
@ -221,10 +219,8 @@ impl Field for Fq2 {
}
fn inverse(&self) -> Option<Self> {
let mut t1 = self.c1;
t1.square();
let mut t0 = self.c0;
t0.square();
let t1 = self.c1.square();
let mut t0 = self.c0.square();
t0.add_assign(&t1);
t0.inverse().map(|t| Fq2 {
c0: self.c0.mul(&t),
@ -257,8 +253,7 @@ impl SqrtField for Fq2 {
0x92c6e9ed90d2eb35,
0x680447a8e5ff9a6,
]);
let mut alpha = a1;
alpha.square();
let mut alpha = a1.square();
alpha.mul_assign(self);
let mut a0 = alpha;
a0.frobenius_map(1);
@ -353,32 +348,30 @@ fn test_fq2_squaring() {
use super::fq::FqRepr;
use ff::PrimeField;
let mut a = Fq2 {
let a = Fq2 {
c0: Fq::one(),
c1: Fq::one(),
}; // u + 1
a.square();
assert_eq!(
a,
a.square(),
Fq2 {
c0: Fq::zero(),
c1: Fq::from_repr(FqRepr::from(2)).unwrap(),
}
); // 2u
let mut a = Fq2 {
let a = Fq2 {
c0: Fq::zero(),
c1: Fq::one(),
}; // u
a.square();
assert_eq!(a, {
assert_eq!(a.square(), {
Fq2 {
c0: Fq::one().neg(),
c1: Fq::zero(),
}
}); // -1
let mut a = Fq2 {
let a = Fq2 {
c0: Fq::from_repr(FqRepr([
0x9c2c6309bbf8b598,
0x4eef5c946536f602,
@ -398,9 +391,8 @@ fn test_fq2_squaring() {
]))
.unwrap(),
};
a.square();
assert_eq!(
a,
a.square(),
Fq2 {
c0: Fq::from_repr(FqRepr([
0xf262c28c538bcf68,

View File

@ -303,35 +303,35 @@ impl Field for Fq6 {
self.c2.mul_assign(&FROBENIUS_COEFF_FQ6_C2[power % 6]);
}
fn square(&mut self) {
let mut s0 = self.c0;
s0.square();
fn square(&self) -> Self {
let s0 = self.c0.square();
let mut ab = self.c0;
ab.mul_assign(&self.c1);
let s1 = ab.double();
let mut s2 = self.c0;
s2.sub_assign(&self.c1);
s2.add_assign(&self.c2);
s2.square();
s2 = s2.square();
let mut bc = self.c1;
bc.mul_assign(&self.c2);
let s3 = bc.double();
let mut s4 = self.c2;
s4.square();
let s4 = self.c2.square();
self.c0 = s3;
self.c0.mul_by_nonresidue();
self.c0.add_assign(&s0);
let mut c0 = s3;
c0.mul_by_nonresidue();
c0.add_assign(&s0);
self.c1 = s4;
self.c1.mul_by_nonresidue();
self.c1.add_assign(&s1);
let mut c1 = s4;
c1.mul_by_nonresidue();
c1.add_assign(&s1);
self.c2 = s1;
self.c2.add_assign(&s2);
self.c2.add_assign(&s3);
self.c2.sub_assign(&s0);
self.c2.sub_assign(&s4);
let mut c2 = s1;
c2.add_assign(&s2);
c2.add_assign(&s3);
c2.sub_assign(&s0);
c2.sub_assign(&s4);
Fq6 { c0, c1, c2 }
}
fn inverse(&self) -> Option<Self> {
@ -340,20 +340,17 @@ impl Field for Fq6 {
c0.mul_assign(&self.c1);
c0 = c0.neg();
{
let mut c0s = self.c0;
c0s.square();
let c0s = self.c0.square();
c0.add_assign(&c0s);
}
let mut c1 = self.c2;
c1.square();
let mut c1 = self.c2.square();
c1.mul_by_nonresidue();
{
let mut c01 = self.c0;
c01.mul_assign(&self.c1);
c1.sub_assign(&c01);
}
let mut c2 = self.c1;
c2.square();
let mut c2 = self.c1.square();
{
let mut c02 = self.c0;
c02.mul_assign(&self.c2);

View File

@ -693,16 +693,15 @@ fn test_fr_mul_assign() {
#[test]
fn test_fr_squaring() {
let mut a = Fr(FrRepr([
let a = Fr(FrRepr([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0x73eda753299d7d47,
]));
assert!(a.is_valid());
a.square();
assert_eq!(
a,
a.square(),
Fr::from_repr(FrRepr([
0xc0d698e7bde077b8,
0xb79a310579e76ec2,
@ -720,14 +719,7 @@ fn test_fr_squaring() {
for _ in 0..1000000 {
// Ensure that (a * a) = a^2
let a = Fr::random(&mut rng);
let mut tmp = a;
tmp.square();
let mut tmp2 = a;
tmp2.mul_assign(&a);
assert_eq!(tmp, tmp2);
assert_eq!(a.square(), a * a);
}
}
@ -830,8 +822,7 @@ fn test_fr_sqrt() {
// Ensure sqrt(a^2) = a or -a
let a = Fr::random(&mut rng);
let nega = a.neg();
let mut b = a;
b.square();
let b = a.square();
let b = b.sqrt().unwrap();
@ -842,10 +833,8 @@ fn test_fr_sqrt() {
// Ensure sqrt(a)^2 = a for random a
let a = Fr::random(&mut rng);
if let Some(mut tmp) = a.sqrt() {
tmp.square();
assert_eq!(a, tmp);
if let Some(tmp) = a.sqrt() {
assert_eq!(a, tmp.square());
}
}
}

View File

@ -97,7 +97,7 @@ impl Engine for Bls12 {
}
}
f.square();
f = f.square();
}
for &mut (p, ref mut coeffs) in &mut pairs {
@ -131,8 +131,7 @@ impl Engine for Bls12 {
}
let mut x = BLS_X;
let mut y0 = r;
y0.square();
let y0 = r.square();
let mut y1 = y0;
exp_by_x(&mut y1, x);
x >>= 1;
@ -185,18 +184,15 @@ impl G2Prepared {
fn doubling_step(r: &mut G2) -> (Fq2, Fq2, Fq2) {
// Adaptation of Algorithm 26, https://eprint.iacr.org/2010/354.pdf
let mut tmp0 = r.x;
tmp0.square();
let mut tmp0 = r.x.square();
let mut tmp1 = r.y;
tmp1.square();
let mut tmp1 = r.y.square();
let mut tmp2 = tmp1;
tmp2.square();
let mut tmp2 = tmp1.square();
let mut tmp3 = tmp1;
tmp3.add_assign(&r.x);
tmp3.square();
tmp3 = tmp3.square();
tmp3.sub_assign(&tmp0);
tmp3.sub_assign(&tmp2);
tmp3 = tmp3.double();
@ -207,18 +203,16 @@ impl G2Prepared {
let mut tmp6 = r.x;
tmp6.add_assign(&tmp4);
let mut tmp5 = tmp4;
tmp5.square();
let tmp5 = tmp4.square();
let mut zsquared = r.z;
zsquared.square();
let zsquared = r.z.square();
r.x = tmp5;
r.x.sub_assign(&tmp3);
r.x.sub_assign(&tmp3);
r.z.add_assign(&r.y);
r.z.square();
r.z = r.z.square();
r.z.sub_assign(&tmp1);
r.z.sub_assign(&zsquared);
@ -234,7 +228,7 @@ impl G2Prepared {
tmp3.mul_assign(&zsquared);
tmp3 = tmp3.double().neg();
tmp6.square();
tmp6 = tmp6.square();
tmp6.sub_assign(&tmp0);
tmp6.sub_assign(&tmp5);
@ -251,18 +245,16 @@ impl G2Prepared {
fn addition_step(r: &mut G2, q: &G2Affine) -> (Fq2, Fq2, Fq2) {
// Adaptation of Algorithm 27, https://eprint.iacr.org/2010/354.pdf
let mut zsquared = r.z;
zsquared.square();
let zsquared = r.z.square();
let mut ysquared = q.y;
ysquared.square();
let ysquared = q.y.square();
let mut t0 = zsquared;
t0.mul_assign(&q.x);
let mut t1 = q.y;
t1.add_assign(&r.z);
t1.square();
t1 = t1.square();
t1.sub_assign(&ysquared);
t1.sub_assign(&zsquared);
t1.mul_assign(&zsquared);
@ -270,8 +262,7 @@ impl G2Prepared {
let mut t2 = t0;
t2.sub_assign(&r.x);
let mut t3 = t2;
t3.square();
let t3 = t2.square();
let t4 = t3.double().double();
@ -288,14 +279,13 @@ impl G2Prepared {
let mut t7 = t4;
t7.mul_assign(&r.x);
r.x = t6;
r.x.square();
r.x = t6.square();
r.x.sub_assign(&t5);
r.x.sub_assign(&t7);
r.x.sub_assign(&t7);
r.z.add_assign(&t2);
r.z.square();
r.z = r.z.square();
r.z.sub_assign(&zsquared);
r.z.sub_assign(&t3);
@ -313,11 +303,10 @@ impl G2Prepared {
r.y = t8;
r.y.sub_assign(&t0);
t10.square();
t10 = t10.square();
t10.sub_assign(&ysquared);
let mut ztsquared = r.z;
ztsquared.square();
let ztsquared = r.z.square();
t10.sub_assign(&ztsquared);

View File

@ -189,8 +189,7 @@ fn test_g1_uncompressed_invalid_vectors() {
let mut x = Fq::one();
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
@ -326,8 +325,7 @@ fn test_g2_uncompressed_invalid_vectors() {
let mut x = Fq2::one();
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
@ -422,8 +420,7 @@ fn test_g1_compressed_invalid_vectors() {
let mut x = Fq::one();
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
@ -447,8 +444,7 @@ fn test_g1_compressed_invalid_vectors() {
let mut x = Fq::one();
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
@ -553,8 +549,7 @@ fn test_g2_compressed_invalid_vectors() {
};
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
@ -585,8 +580,7 @@ fn test_g2_compressed_invalid_vectors() {
};
loop {
let mut x3b = x;
x3b.square();
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),

View File

@ -31,8 +31,7 @@ pub fn random_sqrt_tests<F: SqrtField>() {
for _ in 0..10000 {
let a = F::random(&mut rng);
let mut b = a;
b.square();
let b = a.square();
assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
let b = b.sqrt().unwrap();
@ -43,8 +42,7 @@ pub fn random_sqrt_tests<F: SqrtField>() {
let mut c = F::one();
for _ in 0..10000 {
let mut b = c;
b.square();
let mut b = c.square();
assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue);
b = b.sqrt().unwrap();
@ -218,12 +216,8 @@ fn random_doubling_tests<F: Field, R: RngCore>(rng: &mut R) {
fn random_squaring_tests<F: Field, R: RngCore>(rng: &mut R) {
for _ in 0..10000 {
let mut a = F::random(rng);
let mut b = a;
a.mul_assign(&b);
b.square();
assert_eq!(a, b);
let a = F::random(rng);
assert_eq!(a * a, a.square());
}
}