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

@@ -447,8 +447,7 @@ fn prime_field_constants_and_sqrt(
let mut a1 = self.pow(#mod_minus_3_over_4);
let mut a0 = a1;
a0.square();
let mut a0 = a1.square();
a0.mul_assign(self);
if a0.0 == #repr(#rneg) {
@@ -484,22 +483,21 @@ fn prime_field_constants_and_sqrt(
while t != Self::one() {
let mut i = 1;
{
let mut t2i = t;
t2i.square();
let mut t2i = t.square();
loop {
if t2i == Self::one() {
break;
}
t2i.square();
t2i = t2i.square();
i += 1;
}
}
for _ in 0..(m - i - 1) {
c.square();
c = c.square();
}
r.mul_assign(&c);
c.square();
c = c.square();
t.mul_assign(&c);
m = i;
}
@@ -715,7 +713,9 @@ fn prime_field_impl(
);
gen.extend(quote! {
self.mont_reduce(#mont_calling);
let mut ret = *self;
ret.mont_reduce(#mont_calling);
ret
});
gen
@@ -1113,7 +1113,7 @@ fn prime_field_impl(
}
#[inline]
fn square(&mut self)
fn square(&self) -> Self
{
#squaring_impl
}

View File

@@ -51,7 +51,8 @@ pub trait Field:
fn is_zero(&self) -> bool;
/// Squares this element.
fn square(&mut self);
#[must_use]
fn square(&self) -> Self;
/// Doubles this element.
#[must_use]
@@ -73,7 +74,7 @@ pub trait Field:
for i in BitIterator::new(exp) {
if found_one {
res.square();
res = res.square();
} else {
found_one = i;
}