Move fe_square() into square() and square_mut() in FieldElement.

This commit is contained in:
2019-05-25 16:59:56 -07:00
parent 25746af626
commit ac380d08af
2 changed files with 239 additions and 269 deletions

View File

@@ -568,18 +568,19 @@ fn mul() {
}); });
} }
pub fn fe_square(h: &mut FieldElement, f: &FieldElement) impl FieldElement {
{ pub fn square_mut(&mut self)
let f0 : i32 = f.value[0]; {
let f1 : i32 = f.value[1]; let f0 : i32 = self.value[0];
let f2 : i32 = f.value[2]; let f1 : i32 = self.value[1];
let f3 : i32 = f.value[3]; let f2 : i32 = self.value[2];
let f4 : i32 = f.value[4]; let f3 : i32 = self.value[3];
let f5 : i32 = f.value[5]; let f4 : i32 = self.value[4];
let f6 : i32 = f.value[6]; let f5 : i32 = self.value[5];
let f7 : i32 = f.value[7]; let f6 : i32 = self.value[6];
let f8 : i32 = f.value[8]; let f7 : i32 = self.value[7];
let f9 : i32 = f.value[9]; let f8 : i32 = self.value[8];
let f9 : i32 = self.value[9];
let f0_2 : i32 = 2 * f0; let f0_2 : i32 = 2 * f0;
let f1_2 : i32 = 2 * f1; let f1_2 : i32 = 2 * f1;
let f2_2 : i32 = 2 * f2; let f2_2 : i32 = 2 * f2;
@@ -682,16 +683,24 @@ pub fn fe_square(h: &mut FieldElement, f: &FieldElement)
carry9 = h9 + (1 << 24); h0 += (carry9 >> 25) * 19; h9 -= carry9 & KTOP_39BITS; carry9 = h9 + (1 << 24); h0 += (carry9 >> 25) * 19; h9 -= carry9 & KTOP_39BITS;
carry0 = h0 + (1 << 25); h1 += carry0 >> 26; h0 -= carry0 & KTOP_38BITS; carry0 = h0 + (1 << 25); h1 += carry0 >> 26; h0 -= carry0 & KTOP_38BITS;
h.value[0] = h0 as i32; self.value[0] = h0 as i32;
h.value[1] = h1 as i32; self.value[1] = h1 as i32;
h.value[2] = h2 as i32; self.value[2] = h2 as i32;
h.value[3] = h3 as i32; self.value[3] = h3 as i32;
h.value[4] = h4 as i32; self.value[4] = h4 as i32;
h.value[5] = h5 as i32; self.value[5] = h5 as i32;
h.value[6] = h6 as i32; self.value[6] = h6 as i32;
h.value[7] = h7 as i32; self.value[7] = h7 as i32;
h.value[8] = h8 as i32; self.value[8] = h8 as i32;
h.value[9] = h9 as i32; self.value[9] = h9 as i32;
}
pub fn square(&self) -> FieldElement
{
let mut res = self.clone();
res.square_mut();
res
}
} }
#[cfg(test)] #[cfg(test)]
@@ -705,80 +714,60 @@ fn square() {
assert!(!nega && !negc); assert!(!nega && !negc);
let a = test_from_bytes(&abytes); let a = test_from_bytes(&abytes);
let c = test_from_bytes(&cbytes); let c = test_from_bytes(&cbytes);
let mut r = FieldElement::new(); let r = a.square();
fe_square(&mut r, &a);
assert_eq!(r, c); assert_eq!(r, c);
}); });
} }
pub fn fe_invert(z: &FieldElement) -> FieldElement pub fn fe_invert(z: &FieldElement) -> FieldElement
{ {
let mut t0 = FieldElement::new(); let mut t0 = z.square();
let mut t1 = FieldElement::new(); let mut t1 = t0.square();
let mut t2 = FieldElement::new();
let mut t3 = FieldElement::new();
let mut temp = FieldElement::new();
let mut out = FieldElement::new();
fe_square(&mut t0, &z);
fe_square(&mut t1, &t0);
for _ in 1..2 { for _ in 1..2 {
temp.overwrite_with(&t1); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t1 *= &z; t1 *= &z;
t0 *= &t1; t0 *= &t1;
fe_square(&mut t2, &t0); let mut t2 = t0.square();
t1 *= &t2; t1 *= &t2;
fe_square(&mut t2, &t1); t2 = t1.square();
for _ in 1..5 { for _ in 1..5 {
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t1 *= &t2; t1 *= &t2;
fe_square(&mut t2, &t1); t2 = t1.square();
for _ in 1..10 { for _ in 1..10 {
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t2 *= &t1; t2 *= &t1;
fe_square(&mut t3, &t2); let mut t3 = t2.square();
for _ in 1..20 { for _ in 1..20 {
temp.overwrite_with(&t3); t3.square_mut();
fe_square(&mut t3, &temp);
} }
t2 *= &t3; t2 *= &t3;
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
for _ in 1..10 { for _ in 1..10 {
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t1 *= &t2; t1 *= &t2;
fe_square(&mut t2, &t1); t2 = t1.square();
for _ in 1..50 { for _ in 1..50 {
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t2 *= &t1; t2 *= &t1;
fe_square(&mut t3, &t2); t3 = t2.square();
for _ in 1..100 { for _ in 1..100 {
temp.overwrite_with(&t3); t3.square_mut();
fe_square(&mut t3, &temp);
} }
t2 *= &t3; t2 *= &t3;
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
for _ in 1..50 { for _ in 1..50 {
temp.overwrite_with(&t2); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t1 *= &t2; t1 *= &t2;
temp.overwrite_with(&t1); t1.square_mut();
fe_square(&mut t1, &temp);
for _ in 1..5 { for _ in 1..5 {
temp.overwrite_with(&t1); t1.square_mut();
fe_square(&mut t1, &temp);
} }
&t1 * &t0 &t1 * &t0
} }
@@ -1049,71 +1038,53 @@ fn square2() {
pub fn fe_pow22523(z: &FieldElement) -> FieldElement pub fn fe_pow22523(z: &FieldElement) -> FieldElement
{ {
let mut t0 = FieldElement::new(); let mut t0 = z.square();
let mut t1 = FieldElement::new(); let mut t1 = t0.square();
let mut t2 = FieldElement::new();
let mut temp;
fe_square(&mut t0, &z);
fe_square(&mut t1, &t0);
for _ in 1..2 { for _ in 1..2 {
let temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t1 *= &z; t1 *= &z;
t0 *= &t1; t0 *= &t1;
temp = t0.clone(); t0.square_mut();
fe_square(&mut t0, &temp);
t0 *= &t1; t0 *= &t1;
fe_square(&mut t1, &t0); t1 = t0.square();
for _ in 1..5 { for _ in 1..5 {
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t0 *= &t1; t0 *= &t1;
fe_square(&mut t1, &t0); t1 = t0.square();
for _ in 1..10 { for _ in 1..10 {
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t1 *= &t0; t1 *= &t0;
fe_square(&mut t2, &t1); let mut t2 = t1.square();
for _ in 1..20 { for _ in 1..20 {
temp = t2.clone(); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t1 *= &t2; t1 *= &t2;
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
for _ in 1..10 { for _ in 1..10 {
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t0 *= &t1; t0 *= &t1;
fe_square(&mut t1, &t0); t1 = t0.square();
for _ in 1..50 { for _ in 1..50 {
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t1 *= &t0; t1 *= &t0;
fe_square(&mut t2, &t1); t2 = t1.square();
for _ in 1..100 { for _ in 1..100 {
temp = t2.clone(); t2.square_mut();
fe_square(&mut t2, &temp);
} }
t1 *= &t2; t1 *= &t2;
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
for _ in 1..50 { for _ in 1..50 {
temp = t1.clone(); t1.square_mut();
fe_square(&mut t1, &temp);
} }
t0 *= &t1; t0 *= &t1;
temp = t0.clone(); t0.square_mut();
fe_square(&mut t0, &temp);
for _ in 1..2 { for _ in 1..2 {
temp = t0.clone(); t0.square_mut();
fe_square(&mut t0, &temp);
} }
&t0 * &z &t0 * &z
} }

View File

@@ -47,23 +47,22 @@ impl Point {
let hy = FieldElement::from_bytes(s); let hy = FieldElement::from_bytes(s);
let hz = FieldElement::one(); let hz = FieldElement::one();
fe_square(&mut u, &hy); u = hy.square();
v = &u * &D; v = &u * &D;
temp = u.clone(); temp = u.clone();
u = &temp - &hz; /* u = y^2-1 */ u = &temp - &hz; /* u = y^2-1 */
v += &hz; v += &hz;
fe_square(&mut v3, &v); v3 = v.square();
v3 *= &v; /* v3 = v^3 */ v3 *= &v; /* v3 = v^3 */
let mut hx = FieldElement::zero(); let mut hx = v3.square();
fe_square(&mut hx, &v3);
hx *= &v; hx *= &v;
hx *= &u; /* x = uv^7 */ hx *= &u; /* x = uv^7 */
hx = fe_pow22523(&hx); /* x = (uv^7)^((q-5)/8) */ hx = fe_pow22523(&hx); /* x = (uv^7)^((q-5)/8) */
hx *= &v3; hx *= &v3;
hx *= &u; /* x = uv^3(uv^7)^((q-5)/8) */ hx *= &u; /* x = uv^3(uv^7)^((q-5)/8) */
fe_square(&mut vxx, &hx); vxx = hx.square();
vxx *= &v; vxx *= &v;
let mut check = &vxx - &u; /* vx^2-u */ let mut check = &vxx - &u; /* vx^2-u */
if fe_isnonzero(&check) { if fe_isnonzero(&check) {
@@ -322,11 +321,11 @@ fn ge_p2_dbl(r: &mut PointP1P1, p: &Point2)
{ {
let mut t0 = FieldElement::new(); let mut t0 = FieldElement::new();
fe_square(&mut r.x, &p.x); r.x = p.x.square();
fe_square(&mut r.z, &p.y); r.z = p.y.square();
fe_sq2(&mut r.t, &p.z); fe_sq2(&mut r.t, &p.z);
r.y = &p.x + &p.y; r.y = &p.x + &p.y;
fe_square(&mut t0, &r.y); t0 = r.y.square();
r.y = &r.z + &r.x; r.y = &r.z + &r.x;
r.z -= &r.x; r.z -= &r.x;
r.x = &t0 - &r.y; r.x = &t0 - &r.y;