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

@@ -345,7 +345,7 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
// C = 2*Z1^2
let mut c = self.z;
c.square();
c.double();
c = c.double();
// D = a*A
// = -A

View File

@@ -502,12 +502,16 @@ impl Field for Fs {
}
#[inline]
fn double(&mut self) {
fn double(&self) -> Self {
let mut ret = *self;
// This cannot exceed the backing capacity.
self.0.mul2();
ret.0.mul2();
// However, it may need to be reduced.
self.reduce();
ret.reduce();
ret
}
fn inverse(&self) -> Option<Self> {
@@ -680,7 +684,7 @@ impl Fs {
fn mul_bits<S: AsRef<[u64]>>(&self, bits: BitIterator<S>) -> Self {
let mut res = Self::zero();
for bit in bits {
res.double();
res = res.double();
if bit {
res.add_assign(self)
@@ -1466,11 +1470,8 @@ fn test_fs_double() {
for _ in 0..1000 {
// Ensure doubling a is equivalent to adding a to itself.
let mut a = Fs::random(&mut rng);
let mut b = a;
b.add_assign(&a);
a.double();
assert_eq!(a, b);
let a = Fs::random(&mut rng);
assert_eq!(a.double(), a + a);
}
}

View File

@@ -195,8 +195,7 @@ impl JubjubParams<Bls12> for JubjubBls12 {
impl JubjubBls12 {
pub fn new() -> Self {
let montgomery_a = Fr::from_str("40962").unwrap();
let mut montgomery_2a = montgomery_a;
montgomery_2a.double();
let montgomery_2a = montgomery_a.double();
let mut tmp_params = JubjubBls12 {
// d = -(10240/10241)

View File

@@ -216,19 +216,18 @@ impl<E: JubjubEngine, Subgroup> Point<E, Subgroup> {
{
let mut tmp = *params.montgomery_a();
tmp.mul_assign(&self.x);
tmp.double();
tmp = tmp.double();
delta.add_assign(&tmp);
}
{
let mut tmp = self.x;
tmp.square();
delta.add_assign(&tmp);
tmp.double();
tmp = tmp.double();
delta.add_assign(&tmp);
}
{
let mut tmp = self.y;
tmp.double();
let tmp = self.y.double();
delta.mul_assign(&tmp.inverse().expect("y is nonzero so this must be nonzero"));
}

View File

@@ -314,10 +314,7 @@ fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
{
// Check that 2A is consistent with A
let mut tmp = *params.montgomery_a();
tmp.double();
assert_eq!(&tmp, params.montgomery_2a());
assert_eq!(&params.montgomery_a().double(), params.montgomery_2a());
}
{

View File

@@ -58,7 +58,7 @@ where
if a {
tmp.add_assign(&cur);
}
cur.double(); // 2^1 * cur
cur = cur.double(); // 2^1 * cur
if b {
tmp.add_assign(&cur);
}
@@ -75,9 +75,7 @@ where
if chunks_remaining == 0 {
break;
} else {
cur.double(); // 2^2 * cur
cur.double(); // 2^3 * cur
cur.double(); // 2^4 * cur
cur = cur.double().double().double(); // 2^4 * cur
}
}