From 674a00df5b86e0701cb0bcad8c4234585fc32864 Mon Sep 17 00:00:00 2001 From: Isis Lovecruft Date: Sun, 28 May 2017 22:42:09 +0000 Subject: [PATCH] Some rustfmt fixes. I disagreed with all the other ones. --- src/curve.rs | 40 +++++++++++++++++++++------------------- src/decaf.rs | 35 +++++++++++++++++++---------------- src/field.rs | 12 ++++++------ src/scalar.rs | 33 +++++++++++++++++---------------- src/subtle.rs | 3 +-- 5 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/curve.rs b/src/curve.rs index 5909cba..3fbea08 100644 --- a/src/curve.rs +++ b/src/curve.rs @@ -193,7 +193,7 @@ impl CompressedMontgomeryU { // // XXX any other exceptional points for the birational map? pub fn decompress(&self) -> Option { - let u: FieldElement = FieldElement::from_bytes(&self.0); + let u: FieldElement = FieldElement::from_bytes(&self.0); // If u = -1, then v^2 = u*(u^2+486662*u+1) = 486660. // But 486660 is nonsquare mod p, so this is not a curve point. @@ -317,8 +317,9 @@ impl<'de> Deserialize<'de> for ExtendedPoint { where E: serde::de::Error { if v.len() == 32 { - let arr32 = array_ref!(v,0,32); // &[u8;32] from &[u8] - CompressedEdwardsY(*arr32).decompress() + let arr32 = array_ref!(v, 0, 32); // &[u8;32] from &[u8] + CompressedEdwardsY(*arr32) + .decompress() .ok_or(serde::de::Error::custom("decompression failed")) } else { Err(serde::de::Error::invalid_length(v.len(), &self)) @@ -518,8 +519,8 @@ impl CTAssignable for ExtendedPoint { impl CTEq for ExtendedPoint { fn ct_eq(&self, other: &ExtendedPoint) -> u8 { - arrays_equal( self.compress_edwards().as_bytes(), - other.compress_edwards().as_bytes()) + arrays_equal(self.compress_edwards().as_bytes(), + other.compress_edwards().as_bytes()) } } @@ -551,7 +552,7 @@ impl ProjectivePoint { /// Given (X:Y:Z) in Ɛ, passing to Ɛₑ can be performed in 3M+1S by /// computing (XZ,YZ,XY,Z²). (Note that in that paper, points are /// (X:Y:T:Z) so this really does match the code below). - #[allow(dead_code)] // rustc complains this is unused even when it's used + #[allow(dead_code)] // rustc complains this is unused even when it's used fn to_extended(&self) -> ExtendedPoint { ExtendedPoint{ X: &self.X * &self.Z, @@ -714,7 +715,7 @@ impl ExtendedPoint { // Addition and Subtraction // ------------------------------------------------------------------------ -impl<'a,'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { +impl<'a, 'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { type Output = CompletedPoint; fn add(self, other: &'b ProjectiveNielsPoint) -> CompletedPoint { @@ -735,7 +736,7 @@ impl<'a,'b> Add<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { } } -impl<'a,'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { +impl<'a, 'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { type Output = CompletedPoint; fn sub(self, other: &'b ProjectiveNielsPoint) -> CompletedPoint { @@ -756,7 +757,7 @@ impl<'a,'b> Sub<&'b ProjectiveNielsPoint> for &'a ExtendedPoint { } } -impl<'a,'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint { +impl<'a, 'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint { type Output = CompletedPoint; fn add(self, other: &'b AffineNielsPoint) -> CompletedPoint { @@ -776,7 +777,7 @@ impl<'a,'b> Add<&'b AffineNielsPoint> for &'a ExtendedPoint { } } -impl<'a,'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint { +impl<'a, 'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint { type Output = CompletedPoint; fn sub(self, other: &'b AffineNielsPoint) -> CompletedPoint { @@ -796,7 +797,7 @@ impl<'a,'b> Sub<&'b AffineNielsPoint> for &'a ExtendedPoint { } } -impl<'a,'b> Add<&'b ExtendedPoint> for &'a ExtendedPoint { +impl<'a, 'b> Add<&'b ExtendedPoint> for &'a ExtendedPoint { type Output = ExtendedPoint; fn add(self, other: &'b ExtendedPoint) -> ExtendedPoint { (self + &other.to_projective_niels()).to_extended() @@ -809,7 +810,7 @@ impl<'b> AddAssign<&'b ExtendedPoint> for ExtendedPoint { } } -impl<'a,'b> Sub<&'b ExtendedPoint> for &'a ExtendedPoint { +impl<'a, 'b> Sub<&'b ExtendedPoint> for &'a ExtendedPoint { type Output = ExtendedPoint; fn sub(self, other: &'b ExtendedPoint) -> ExtendedPoint { (self - &other.to_projective_niels()).to_extended() @@ -1193,8 +1194,9 @@ pub mod vartime { /// /// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an /// error to call this function with two vectors of different lengths. - pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> ExtendedPoint - where I: IntoIterator, J: IntoIterator + pub fn k_fold_scalar_mult<'a, 'b, I, J>(scalars: I, points: J) -> ExtendedPoint + where I: IntoIterator, + J: IntoIterator { //assert_eq!(scalars.len(), points.len()); @@ -1652,7 +1654,7 @@ mod test { // CBOR apparently has two bytes of overhead for a 32-byte string. // Set the low byte of the compressed point to 1 to make it invalid. output[2] = 1; - let parsed: Result = serde_cbor::from_slice(&output); + let parsed: Result = serde_cbor::from_slice(&output); assert!(parsed.is_err()); } } @@ -1667,7 +1669,7 @@ mod bench { use test::Bencher; use constants; use super::*; - use super::test::{A_SCALAR}; + use super::test::A_SCALAR; #[bench] fn edwards_decompress(b: &mut Bencher) { @@ -1734,21 +1736,21 @@ mod bench { fn projective_double_output_completed(b: &mut Bencher) { let p1 = constants::ED25519_BASEPOINT.to_projective(); - b.iter(|| p1.double() ); + b.iter(|| p1.double()); } #[bench] fn extended_double_output_extended(b: &mut Bencher) { let p1 = constants::ED25519_BASEPOINT; - b.iter(|| p1.double() ); + b.iter(|| p1.double()); } #[bench] fn mult_by_cofactor(b: &mut Bencher) { let p1 = constants::ED25519_BASEPOINT; - b.iter(|| p1.mult_by_cofactor() ); + b.iter(|| p1.mult_by_cofactor()); } #[cfg(feature="basepoint_table_creation")] diff --git a/src/decaf.rs b/src/decaf.rs index ecaa616..08abe89 100644 --- a/src/decaf.rs +++ b/src/decaf.rs @@ -60,7 +60,7 @@ pub struct CompressedDecaf(pub [u8; 32]); /// The result of compressing a `DecafPoint`. impl CompressedDecaf { /// View this `CompressedDecaf` as an array of bytes. - pub fn as_bytes<'a>(&'a self) -> &'a [u8;32] { + pub fn as_bytes<'a>(&'a self) -> &'a [u8; 32] { &self.0 } @@ -121,7 +121,7 @@ impl CompressedDecaf { impl Identity for CompressedDecaf { fn identity() -> CompressedDecaf { - CompressedDecaf([0u8;32]) + CompressedDecaf([0u8; 32]) } } @@ -165,8 +165,9 @@ impl<'de> Deserialize<'de> for DecafPoint { where E: serde::de::Error { if v.len() == 32 { - let arr32 = array_ref!(v,0,32); // &[u8;32] from &[u8] - CompressedDecaf(*arr32).decompress() + let arr32 = array_ref!(v, 0, 32); // &[u8;32] from &[u8] + CompressedDecaf(*arr32) + .decompress() .ok_or(serde::de::Error::custom("decompression failed")) } else { Err(serde::de::Error::invalid_length(v.len(), &self)) @@ -193,10 +194,10 @@ impl DecafPoint { pub fn compress(&self) -> CompressedDecaf { // Q: Do we want to encode twisted or untwisted? // - // Notes: + // Notes: // Recall that the twisted Edwards curve E_{a,d} is of the form // - // ax^2 + y^2 = 1 + dx^2y^2. + // ax^2 + y^2 = 1 + dx^2y^2. // // Internally, we operate on the curve with a = -1, d = // -121665/121666, a.k.a., the twist. But maybe we would like @@ -205,7 +206,7 @@ impl DecafPoint { // // Fix i, a square root of -1 (mod p). // - // The map x -> ix is an isomorphism from E_{a,d} to E_{-a,-d}. + // The map x -> ix is an isomorphism from E_{a,d} to E_{-a,-d}. // Its inverse is x -> -ix. // let untwisted_X = &self.X * &constants::MSQRT_M1; // etc. @@ -247,7 +248,7 @@ impl DecafPoint { // // 0 = (-X^2 + Y^2)*Z^2 - Z^4 - d*X^2*Y^2, // - // so + // so // 0 = (-X^2 + Y^2)*Z^2 - Z^4 - d*T^2*Z^2 since XY=TZ // = (-X^2 + Y^2 - Z^2 - d*T^2)*Z^2 // = ( X^2 - Y^2 + Z^2 + d*T^2)*Z^2 mult by -1 @@ -304,7 +305,7 @@ impl DecafPoint { let (tmp_is_nonzero_square, W) = tmp.invsqrt(); // tmp should always be a square (why? related to being in the // image of the isogeny?) - debug_assert_eq!( tmp_is_nonzero_square | tmp.is_zero(), 1u8 ); + debug_assert_eq!(tmp_is_nonzero_square | tmp.is_zero(), 1u8); let xy = &T.square() * &(&W.square() * &(&TZ * &ZZ_plus_XX)); let rotate = 1u8 & !(Y.is_nonzero() & xy.is_nonnegative_decaf()); @@ -363,7 +364,7 @@ impl DecafPoint { let r_0_squared = r_0.square(); let r = &r_0_squared + &r_0_squared; - // 2. Compute D <--- (dr + (a-d)) * (dr - (d + ar)) + // 2. Compute D <--- (dr + (a-d)) * (dr - (d + ar)) let dr = &constants::d * &r; // D = (dr + (a-d)) * (dr - (d + ar)) // = (dr + (a-d)) * (dr - (d-r)) since a=-1 @@ -398,7 +399,7 @@ impl DecafPoint { s *= &c; // 6. Compute t <--- -c*N*(r-1)* ((a-2d)*e)^2 -1 - let a_minus_2d_e_sq = (&(&minus_one-&constants::d2)*&e).square(); + let a_minus_2d_e_sq = (&(&minus_one - &constants::d2) * &e).square(); let c_N_r_minus_1 = &c * &(&N * &(&r + &minus_one)); let t = &minus_one - &(&c_N_r_minus_1 * &a_minus_2d_e_sq); @@ -467,7 +468,8 @@ impl DecafPoint { /// ``` /// pub fn hash_from_bytes(input: &[u8]) -> DecafPoint - where D: Digest + Default { + where D: Digest + Default + { let mut hash = D::default(); hash.input(input); DecafPoint::from_hash(hash) @@ -479,7 +481,8 @@ impl DecafPoint { /// to stream data into the `Digest` than to pass a single byte /// slice. pub fn from_hash(hash: D) -> DecafPoint - where D: Digest + Default { + where D: Digest + Default + { // XXX this seems clumsy let mut output = [0u8; 32]; output.copy_from_slice(hash.result().as_slice()); @@ -674,8 +677,9 @@ pub mod vartime { /// /// A vector of `Scalar`s and a vector of `ExtendedPoints`. It is an /// error to call this function with two vectors of different lengths. - pub fn k_fold_scalar_mult<'a,'b,I,J>(scalars: I, points: J) -> DecafPoint - where I: IntoIterator, J: IntoIterator + pub fn k_fold_scalar_mult<'a, 'b, I, J>(scalars: I, points: J) -> DecafPoint + where I: IntoIterator, + J: IntoIterator { let extended_points = points.into_iter().map(|P| &P.0); DecafPoint(curve::vartime::k_fold_scalar_mult(scalars, extended_points)) @@ -836,4 +840,3 @@ mod bench { b.iter(|| P.compress()); } } - diff --git a/src/field.rs b/src/field.rs index ef36f28..57aae65 100644 --- a/src/field.rs +++ b/src/field.rs @@ -116,7 +116,7 @@ impl Index for FieldElement { impl IndexMut for FieldElement { fn index_mut(&mut self, _index: usize) -> &mut Limb { - &mut(self.0[_index]) + &mut (self.0[_index]) } } @@ -456,7 +456,7 @@ impl FieldElement { #[cfg(not(feature="radix_51"))] fn reduce(mut h: [i64; 10]) -> FieldElement { //FeCombine - let mut c = [0i64;10]; + let mut c = [0i64; 10]; /* |h[0]| <= (1.1*1.1*2^52*(1+19+19+19+19)+1.1*1.1*2^50*(38+38+38+38+38)) @@ -709,7 +709,7 @@ impl FieldElement { // evidently 2^255 h10-2^255 q = 0. // Goal: Output h[0]+...+2^230 h[9]. - let mut s = [0u8;32]; + let mut s = [0u8; 32]; s[0] = (h[0] >> 0) as u8; s[1] = (h[0] >> 8) as u8; s[2] = (h[0] >> 16) as u8; @@ -1179,7 +1179,7 @@ impl FieldElement { let r_prime = &constants::SQRT_M1 * &r; r.conditional_assign(&r_prime, flipped_sign_sqrt); - + let was_nonzero_square = correct_sign_sqrt | flipped_sign_sqrt; (was_nonzero_square, r) @@ -1263,7 +1263,7 @@ mod test { fn a_mul_a_vs_a_squared_constant() { let a = FieldElement::from_bytes(&A_BYTES); let asq = FieldElement::from_bytes(&ASQ_BYTES); - assert_eq!(asq, &a*&a); + assert_eq!(asq, &a * &a); } #[test] @@ -1381,7 +1381,7 @@ mod bench { #[bench] fn fieldelement_a_mul_a(b: &mut Bencher) { let a = FieldElement::from_bytes(&A_BYTES); - b.iter(|| &a*&a); + b.iter(|| &a * &a); } #[bench] diff --git a/src/scalar.rs b/src/scalar.rs index 3f4a6cc..ed38c0d 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -63,7 +63,7 @@ impl Debug for Scalar { } } -impl Eq for Scalar{} +impl Eq for Scalar {} impl PartialEq for Scalar { /// Test equality between two `Scalar`s. /// @@ -101,7 +101,7 @@ impl Index for Scalar { impl IndexMut for Scalar { fn index_mut(&mut self, _index: usize) -> &mut u8 { - &mut(self.0[_index]) + &mut (self.0[_index]) } } @@ -151,7 +151,7 @@ impl<'a> Neg for &'a Scalar { type Output = Scalar; fn neg(self) -> Scalar { self * &constants::l_minus_1 - } + } } impl CTAssignable for Scalar { @@ -218,7 +218,7 @@ impl<'de> Deserialize<'de> for Scalar { { if v.len() == 32 { // array_ref turns &[u8] into &[u8;32] - Ok(Scalar(*array_ref!(v,0,32))) + Ok(Scalar(*array_ref!(v, 0, 32))) } else { Err(serde::de::Error::invalid_length(v.len(), &self)) } @@ -270,7 +270,8 @@ impl Scalar { /// ``` /// pub fn hash_from_bytes(input: &[u8]) -> Scalar - where D: Digest + Default { + where D: Digest + Default + { let mut hash = D::default(); hash.input(input); Scalar::from_hash(hash) @@ -282,9 +283,10 @@ impl Scalar { /// to stream data into the `Digest` than to pass a single byte /// slice. pub fn from_hash(hash: D) -> Scalar - where D: Digest + Default { + where D: Digest + Default + { // XXX this seems clumsy - let mut output = [0u8;64]; + let mut output = [0u8; 64]; output.copy_from_slice(hash.result().as_slice()); Scalar::reduce(&output) } @@ -320,7 +322,7 @@ impl Scalar { } /// Get the bits of the scalar. - pub fn bits(&self) -> [i8;256] { + pub fn bits(&self) -> [i8; 256] { let mut bits = [0i8; 256]; for i in 0..256 { // As i runs from 0..256, the bottom 3 bits index the bit, @@ -379,7 +381,7 @@ impl Scalar { // Unpack a scalar into 12 21-bit limbs. fn unpack(&self) -> UnpackedScalar { - let mask_21bits: i64 = (1 << 21) -1; + let mask_21bits: i64 = (1 << 21) - 1; let mut a = UnpackedScalar([0i64; 12]); a[ 0] = mask_21bits & load3(&self.0[ 0..]) ; a[ 1] = mask_21bits & (load4(&self.0[ 2..]) >> 5); @@ -504,7 +506,7 @@ impl Index for UnpackedScalar { impl IndexMut for UnpackedScalar { fn index_mut(&mut self, _index: usize) -> &mut i64 { - &mut(self.0[_index]) + &mut (self.0[_index]) } } @@ -616,7 +618,7 @@ impl UnpackedScalar { /// 2^252 = -27742317777372353535851937790883648493 (mod l). /// /// We can write the right-hand side in 21-bit limbs as - /// + /// /// rhs = 666643 * 2^0 /// + 470296 * 2^21 /// + 654183 * 2^42 @@ -640,7 +642,7 @@ impl UnpackedScalar { fn reduce_limbs(mut limbs: &mut [i64; 24]) -> UnpackedScalar { #[inline] #[allow(dead_code)] - fn do_reduction(limbs: &mut [i64; 24], i:usize) { + fn do_reduction(limbs: &mut [i64; 24], i: usize) { limbs[i - 12] += limbs[i] * 666643; limbs[i - 11] += limbs[i] * 470296; limbs[i - 10] += limbs[i] * 654183; @@ -662,7 +664,7 @@ impl UnpackedScalar { #[allow(dead_code)] /// Carry excess from the `i`-th limb into the `(i+1)`-th limb. /// Postcondition: `-2^20 <= limbs[i] < 2^20`. - fn do_carry_centered(limbs: &mut [i64; 24], i:usize) { + fn do_carry_centered(limbs: &mut [i64; 24], i: usize) { let carry: i64 = (limbs[i] + (1<<20)) >> 21; limbs[i+1] += carry; limbs[i ] -= carry << 21; @@ -717,7 +719,6 @@ impl UnpackedScalar { UnpackedScalar(*array_ref!(limbs, 0, 12)) } - } #[cfg(test)] @@ -901,7 +902,7 @@ mod bench { #[bench] fn scalar_multiply_add(b: &mut Bencher) { - b.iter(|| Scalar::multiply_add(&X, &Y, &Z) ); + b.iter(|| Scalar::multiply_add(&X, &Y, &Z)); } #[bench] @@ -915,6 +916,6 @@ mod bench { let x = X.unpack(); let y = Y.unpack(); let z = Z.unpack(); - b.iter(|| UnpackedScalar::multiply_add(&x, &y, &z) ); + b.iter(|| UnpackedScalar::multiply_add(&x, &y, &z)); } } diff --git a/src/subtle.rs b/src/subtle.rs index 9770951..5d024bc 100644 --- a/src/subtle.rs +++ b/src/subtle.rs @@ -35,8 +35,7 @@ pub trait CTEq { /// /// Note: it is not necessary to implement this trait, as a generic /// implementation is provided. -pub trait CTNegatable -{ +pub trait CTNegatable { /// Conditionally negate an element if `choice == 1u8`. fn conditional_negate(&mut self, choice: u8); }