diff --git a/src/ed25519/fe.rs b/src/ed25519/fe.rs index dd65946..be6a42e 100644 --- a/src/ed25519/fe.rs +++ b/src/ed25519/fe.rs @@ -821,13 +821,15 @@ fn negate() { }); } -pub fn fe_cmov(f: &mut FieldElement, g: &FieldElement, bl: bool) -{ - let b = if bl { -1 } else { 0 }; - for i in 0..10 { - let mut x = f.value[i] ^ g.value[i]; - x &= b; - f.value[i] ^= x; +impl FieldElement { + pub fn cmov(&mut self, g: &FieldElement, bl: bool) + { + let b = if bl { -1 } else { 0 }; + for i in 0..10 { + let mut x = self.value[i] ^ g.value[i]; + x &= b; + self.value[i] ^= x; + } } } @@ -845,25 +847,27 @@ fn cmov() { let b = bbytes.len() > 1; let c = test_from_bytes(&cbytes); let mut r = FieldElement::new(); - fe_cmov(&mut r, &a, b); + r.cmov(&a, b); assert_eq!(r, c); }); } -pub fn fe_isnonzero(f: &FieldElement) -> bool -{ - let s = f.to_bytes(); - let mut res = false; - for i in 0..32 { - res |= s[i] != 0; +impl FieldElement { + pub fn isnonzero(&self) -> bool + { + let s = self.to_bytes(); + let mut res = false; + for i in 0..32 { + res |= s[i] != 0; + } + res } - res -} -pub fn fe_isnegative(f: &FieldElement) -> bool -{ - let s = f.to_bytes(); - s[0] & 1 == 1 + pub fn isnegative(&self) -> bool + { + let s = self.to_bytes(); + s[0] & 1 == 1 + } } #[cfg(test)] @@ -880,8 +884,8 @@ fn is_tests() { println!("a: {:?}", a); let z = zbytes.len() > 1; let n = nbytes.len() > 1; - assert_eq!(z, fe_isnonzero(&a)); - assert_eq!(n, fe_isnegative(&a)); + assert_eq!(z, a.isnonzero()); + assert_eq!(n, a.isnegative()); }); } diff --git a/src/ed25519/point.rs b/src/ed25519/point.rs index 3653b0c..838c268 100644 --- a/src/ed25519/point.rs +++ b/src/ed25519/point.rs @@ -58,15 +58,15 @@ impl Point { let mut vxx = hx.square(); vxx *= &v; let mut check = &vxx - &u; /* vx^2-u */ - if fe_isnonzero(&check) { + if check.isnonzero() { check = &vxx + &u; - if fe_isnonzero(&check) { + if check.isnonzero() { return None; } hx *= &SQRTM1; } - if fe_isnegative(&hx) != ((s[31] >> 7) == 1) { + if hx.isnegative() != ((s[31] >> 7) == 1) { hx = -&hx; } @@ -473,9 +473,9 @@ fn equal(b: i8, c: i8) -> bool fn cmov(t: &mut Precomp, u: &Precomp, b: bool) { - fe_cmov(&mut t.yplusx, &u.yplusx, b); - fe_cmov(&mut t.yminusx, &u.yminusx, b); - fe_cmov(&mut t.xy2d, &u.xy2d, b); + t.yplusx.cmov(&u.yplusx, b); + t.yminusx.cmov(&u.yminusx, b); + t.xy2d.cmov(&u.xy2d, b); } fn negative(b: i8) -> u8 @@ -1796,7 +1796,7 @@ fn into_encoded_point(x: &FieldElement, y: &FieldElement, z: &FieldElement) -> V let x_over_z = x * &recip; let y_over_z = y * &recip; let mut bytes = y_over_z.to_bytes(); - let sign_bit = if fe_isnegative(&x_over_z) { 1 } else { 0 }; + let sign_bit = if x_over_z.isnegative() { 1 } else { 0 }; // The preceding computations must execute in constant time, but this // doesn't need to. bytes[31] ^= sign_bit << 7;