Make Field::square take &self and return Self

This commit is contained in:
Jack Grigg
2019-12-12 23:09:28 +00:00
parent 9dac748224
commit cded08b0c5
24 changed files with 160 additions and 272 deletions

View File

@@ -107,8 +107,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
// as dy^2 + 1 = 0 has no solution in Fr.
// tmp1 = y^2
let mut tmp1 = y;
tmp1.square();
let mut tmp1 = y.square();
// tmp2 = (y^2 * d) + 1
let mut tmp2 = tmp1;
@@ -335,17 +334,13 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
// http://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
// 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 = 2*Z1^2
let mut c = self.z;
c.square();
c = c.double();
let c = self.z.square().double();
// D = a*A
// = -A
@@ -354,7 +349,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
// E = (X1+Y1)^2 - A - B
let mut e = self.x;
e.add_assign(&self.y);
e.square();
e = e.square();
e.add_assign(&d); // -A = D
e.sub_assign(&b);

View File

@@ -575,7 +575,7 @@ impl Field for Fs {
}
#[inline]
fn square(&mut self) {
fn square(&self) -> Self {
let mut carry = 0;
let r1 = mac_with_carry(0, (self.0).0[0], (self.0).0[1], &mut carry);
let r2 = mac_with_carry(0, (self.0).0[0], (self.0).0[2], &mut carry);
@@ -606,7 +606,10 @@ impl Field for Fs {
let r5 = adc(r5, 0, &mut carry);
let r6 = mac_with_carry(r6, (self.0).0[3], (self.0).0[3], &mut carry);
let r7 = adc(r7, 0, &mut carry);
self.mont_reduce(r0, r1, r2, r3, r4, r5, r6, r7);
let mut ret = *self;
ret.mont_reduce(r0, r1, r2, r3, r4, r5, r6, r7);
ret
}
}
@@ -736,8 +739,7 @@ impl SqrtField for Fs {
0x4199cec0404d0ec0,
0x39f6d3a994cebea,
]);
let mut a0 = a1;
a0.square();
let mut a0 = a1.square();
a0.mul_assign(self);
if a0 == NEGATIVE_ONE {
@@ -1403,16 +1405,15 @@ fn test_fs_mul_assign() {
#[test]
fn test_fr_squaring() {
let mut a = Fs(FsRepr([
let a = Fs(FsRepr([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0xe7db4ea6533afa8,
]));
assert!(a.is_valid());
a.square();
assert_eq!(
a,
a.square(),
Fs::from_repr(FsRepr([
0x12c7f55cbc52fbaa,
0xdedc98a0b5e6ce9e,
@@ -1431,8 +1432,7 @@ fn test_fr_squaring() {
// Ensure that (a * a) = a^2
let a = Fs::random(&mut rng);
let mut tmp = a;
tmp.square();
let tmp = a.square();
let mut tmp2 = a;
tmp2.mul_assign(&a);
@@ -1538,8 +1538,7 @@ fn test_fs_sqrt() {
// Ensure sqrt(a^2) = a or -a
let a = Fs::random(&mut rng);
let nega = a.neg();
let mut b = a;
b.square();
let b = a.square();
let b = b.sqrt().unwrap();
@@ -1550,10 +1549,8 @@ fn test_fs_sqrt() {
// Ensure sqrt(a)^2 = a for random a
let a = Fs::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

@@ -50,8 +50,7 @@ impl<E: JubjubEngine> Point<E, Unknown> {
pub fn get_for_x(x: E::Fr, sign: bool, params: &E::Params) -> Option<Self> {
// Given an x on the curve, y = sqrt(x^3 + A*x^2 + x)
let mut x2 = x;
x2.square();
let mut x2 = x.square();
let mut rhs = x2;
rhs.mul_assign(params.montgomery_a());
@@ -220,8 +219,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
delta.add_assign(&tmp);
}
{
let mut tmp = self.x;
tmp.square();
let mut tmp = self.x.square();
delta.add_assign(&tmp);
tmp = tmp.double();
delta.add_assign(&tmp);
@@ -231,8 +229,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
delta.mul_assign(&tmp.inverse().expect("y is nonzero so this must be nonzero"));
}
let mut x3 = delta;
x3.square();
let mut x3 = delta.square();
x3.sub_assign(params.montgomery_a());
x3.sub_assign(&self.x);
x3.sub_assign(&self.x);
@@ -281,8 +278,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
);
}
let mut x3 = delta;
x3.square();
let mut x3 = delta.square();
x3.sub_assign(params.montgomery_a());
x3.sub_assign(&self.x);
x3.sub_assign(&other.x);

View File

@@ -20,11 +20,9 @@ pub fn test_suite<E: JubjubEngine>(params: &E::Params) {
}
fn is_on_mont_curve<E: JubjubEngine, P: JubjubParams<E>>(x: E::Fr, y: E::Fr, params: &P) -> bool {
let mut lhs = y;
lhs.square();
let lhs = y.square();
let mut x2 = x;
x2.square();
let x2 = x.square();
let mut x3 = x2;
x3.mul_assign(&x);
@@ -42,11 +40,9 @@ fn is_on_twisted_edwards_curve<E: JubjubEngine, P: JubjubParams<E>>(
y: E::Fr,
params: &P,
) -> bool {
let mut x2 = x;
x2.square();
let x2 = x.square();
let mut y2 = y;
y2.square();
let y2 = y.square();
// -x^2 + y^2
let mut lhs = y2;
@@ -346,8 +342,7 @@ fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
{
// Check that A^2 - 4 is nonsquare:
let mut tmp = params.montgomery_a().clone();
tmp.square();
let mut tmp = params.montgomery_a().square();
tmp.sub_assign(&E::Fr::from_str("4").unwrap());
assert!(tmp.legendre() == LegendreSymbol::QuadraticNonResidue);
}